##### R CODE TO REPLICATE THE STATISTICAL ANALYSES AND FIGURES OF
##### Adrian Lucardi, Juan Pablo Micozzi and Natan Skigin, "Resignation as Promotion? Executive Turnover and Early Departures in the Argentine Congress, 1983-2017" Forthcoming at Legislative Studies Quarterly

##### PART I: MONTHLY DATA
##### The following analysis were run with R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"


## emptying the workspace
rm (list=ls ())


## saving the output to a log file
sink (file="Log Analysis Resignation as Promotion 01 - Monthly.txt", append=FALSE, type="output", split=FALSE)


## loading the required R packages. This code installs all required packages automativally
if (!require ("pacman")) install.packages ("pacman")
pacman::p_load (
  colourvalues
  , did
  , dplyr
  , DRDID
  , ggplot2
  , ggrepel
  , jtools
  , lfe
  , readxl
  , survival
  , stringr
  , tidyr
  , tidyverse
  , wesanderson
  , xtable)


## display options
options (digits=4
         , scipen=8
         , show.signif.stars=FALSE
         , tibble.width=Inf
         , tibble.print_max=Inf
         , tibble.print_min=1)


## setting working directory --> replace this with path to your own working directory
home <- "~/Dropbox/Current projects/Resignation/"
setwd (home)


## setting the graphical parameters
movie <- wes_palette ("Cavalcanti1")
movie_labels <- wes_palette ("Rushmore") ## movie colors for the labels
cex_base <- 9
cex_legend <- 6.5
e <- 3.5 ## distance of the labels in the plot
digits <- 2 ## number of decimals to use in the tables


## function for adding parentheses automatically
parSE <- function (x, n.digits){
  txt <- str_c ("%.", n.digits, "f")
  x.tmp <- matrix (sprintf (as.character (txt), as.matrix (x, n.digits)), ncol=ncol (x), byrow=F)
  x.tmp[seq (2, nrow (x.tmp), by=2),] <- paste ("(", x.tmp[seq (2, nrow (x.tmp), by=2),], ")", sep="")
  x.tmp <- sub ("\\(NA\\)", x.tmp, replacement="")
  x.tmp <- sub ("NA", x.tmp, replacement="")
  return (x.tmp)  }


## working max() and min() functions, not the b.s. introduced in R 3.2.2
max.na <- function (x) {
  if (all (is.na (x))==TRUE){ return (NA) 
  } else {return (max (x, na.rm=TRUE))} }
min.na <- function (x) {
  if (all (is.na (x))==TRUE){ return (NA) 
  } else {return (min (x, na.rm=TRUE))} } 




#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 1 // DOWNLOADING THE DATA ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

## legislator-mandate
load (file="data/Legislator-mandate full 1983-2017.RData")
summary (lman); dim (lman)


## legislator-month
load (file="data/Legislator-month full 1983-2017.RData")
summary (lmonth); dim (lmonth)


## legislator-day --> be careful; 3.1 million obs
load (file="data/Legislator-day full 1983-2017.RData")
summary (lday2); dim (lday2)


## political careers in Argentina -> we want to show that reelection is much more common for other offices than for national legislators
cars <- read_xlsx ("data/Careers (all) 1983-2017.xlsx", na="NA")
summary (cars); dim (cars)





#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 2 // DESCRIPTIVE STATISTICS ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

#### (2.1) Illustrative examples included in the text

### (2.1.1) sample sizes ####
nrow (filter (lman, assumed==1)) ## 2483
nrow (filter (lman, assumed==1 & resignation==1)) ## 267

nrow (filter (lman, elected==1 & assumed==1)) ## 2126
nrow (filter (lman, elected==1 & assumed==1 & resignation==1))  ## 255
round (nrow (filter (lman, elected==1 & assumed==1 & resignation==1)) / nrow (filter (lman, elected==1 & assumed==1))*100,1) ## 12%



### (2.1.2) 1997 and 2013 ballots in CABA ####

## Alianza, 1997
arrange (select (filter (lman, district=="CABA" & period=="1997-2001" & agrupacion=="ALIANZA POR EL TRABAJO LA JUSTICIA Y LA EDUCACION"), district, agrupacion, period, name, titular, position, elected, assumed, finished, resignation, death, officeNext), position)
nrow (filter (lman, district=="CABA" & period=="1997-2001" & agrupacion=="ALIANZA POR EL TRABAJO LA JUSTICIA Y LA EDUCACION"))


## Union-PRO, 2013
arrange (select (filter (lman, district=="CABA" & period=="2013-2017" & agrupacion=="ALIANZA UNION-PRO"), district, agrupacion, period, name, titular, position, elected, assumed, finished, resignation, death, officeNext), position)
nrow (filter (lman, district=="CABA" & period=="2013-2017" & agrupacion=="ALIANZA UNION-PRO"))



### (2.1.3) Where do resignees go? ####

# dataset of resignees
lman <- lman %>% ungroup ()
resigs <- select (filter (lman, elected==1 & assumed==1 & resignation==1), district, period, name, agrupacion, party, party2, officeNext, officeNext_level, officeNext_type2, officeNext_type, officeNext_full)
(n_resig <- nrow (resigs))  ## 255

# getting the numbers
(tabResigs <- rbind (
  c (nrow (filter (resigs, officeNext_level=="national" & officeNext_type=="elected-executive")), nrow (filter (resigs, officeNext_level=="national" & officeNext_type=="elected-legislative")), NA, NA, nrow (filter (resigs, officeNext_level=="national" & officeNext_type2=="appointed")), NA, NA)
  , c (nrow (filter (resigs, officeNext_level=="subnational" & officeNext_type=="elected-executive")), nrow (filter (resigs, officeNext_level=="subnational" & officeNext_type=="elected-legislative")), NA, NA, nrow (filter (resigs, officeNext_level=="subnational" & officeNext_type2=="appointed")), NA, NA)
  , c (nrow (filter (resigs, officeNext_type=="elected-executive")), nrow (filter (resigs, officeNext_type=="elected-legislative")), NA, NA, nrow (filter (resigs, officeNext_type2=="appointed")), NA, NA)))
tabResigs[,3] <- tabResigs[,1] + tabResigs[,2]
tabResigs[,7] <- tabResigs[,3] + tabResigs[,5]
tabResigs <- rbind (tabResigs, matrix (paste0 ("(", sprintf ("%.1f", round (tabResigs/n_resig*100, 1)), "\\%)"), nrow=3, byrow=F))[c (1,4,2,5,3,6),]
tabResigs[is.na(tabResigs)] <- ""
tabResigs[tabResigs=="(NA\\%)"] <- ""
tabResigs ## all's well

# building the table proper
rows <- c ("national position", ""
           , "[0.85ex] subnational position", ""
           , "[0.85ex] total", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{elected}} & & \\multicolumn{1}{c}{\\textbf{appointed}} & & \\multicolumn{1}{c}{\\textbf{all}} \\\\ \\cmidrule{2-4} \\cmidrule{6-6} \\cmidrule{8-8} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{executive} & \\multicolumn{1}{c}{legislative} & \\multicolumn{1}{c}{total} & & & & \\\\ \\midrule \n")
Header3 <- paste0 ("\\midrule \\multicolumn{8}{l}{retired / no new position immediately after resignation: ", sprintf ("%.0f", round (nrow (filter (resigs, officeNext=="none")),0)), " (", sprintf ("%.1f", round (nrow (filter (resigs, officeNext=="none"))/n_resig*100,1)), "\\%)} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 6
addtorow$command <- c (Header1, Header2, Header3)
print (xtable ( cbind (rows, tabResigs)
                , align=c ('l','l','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Future (immediate) career paths of deputies who resigned, Argentina, 1983-2017"
                , label="T:resignees")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )




#### (2.2) What happens in t+4 if you're elected in t ####

## preparing the data
lman <- lman %>% mutate (
  next4 = ifelse (elected==0, NA, ifelse (
    assumed==0, "did not\nassume", ifelse (
      death==1 | removal==1, "death/\nexpulsion", ifelse (
        resignation==1, "resignation", ifelse (
          finished==1 & officeNext=="dipnac", "finish-\nreelection", ifelse (
            finished==1 & officeNext!="none", "finish-\nanother job", "finish-\nnothing else"))))))
  , next4 = factor (next4, levels=c (
    "did not\nassume", "death/\nexpulsion", "resignation", "finish-\nreelection", "finish-\nanother job", "finish-\nnothing else"))
  , previous_exp = ifelse (
    elected==0, NA, ifelse (
      any.b==0, "none", ifelse (
        exe.b==1 & leg.b==1, "both executive and legislative", ifelse (
          exe.b==1, "only executive", "only legislative"))))
  , previous_exp = factor (previous_exp, levels=c (
    "both executive and legislative", "only executive", "only legislative", "none"))
  , chair_dum = ifelse (chair > 0, 1, 0)
  , party = factor (party, levels=c ("PJ", "UCR", "3rd party", "other")) )



### (2.2.1) by position in party list ####
filter (lman, !is.na (next4)) %>% group_by (next4, positionOrd) %>% summarise (n = n()) %>% group_by (positionOrd) %>% mutate (freq = n / sum(n)*100) ## raw values

(pNext4_list <- ggplot (
  (filter (lman, !is.na (next4)) %>% group_by (next4, positionOrd) %>% summarise (n = n()) %>% group_by (positionOrd) %>% mutate (freq = n / sum(n)*100))
  , aes (x=next4, y=freq, fill=factor(positionOrd)))
  + geom_col (position="dodge")
  + ylim (0, 70)
  + xlab ("")
  + ylab ("% of elected deputies")
  + scale_fill_manual (values=movie, labels=c (
    str_c ("placed 1st in list\n(", round (mean (filter (lman, !is.na (next4))$top01)*100, 0), "% of sample)")
    , str_c ("placed 2nd in list\n (", round (mean (filter (lman, !is.na (next4))$top02)*100, 0), "% of sample)")
    , str_c ("placed 3rd or lower in list\n(", round ((1 - mean (filter (lman, !is.na (next4))$top01) - mean (filter (lman, !is.na (next4))$top02))*100, 0), "% of sample)")))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-15,-9,-9,-9)))


## (2.2.2) by electoral vulnerability ####
filter (lman, !is.na (next4)) %>% group_by (next4, vulnerable) %>% summarise (n = n()) %>% group_by (vulnerable) %>% mutate (freq = n / sum(n)*100)

(pNext4_vulnerable <- ggplot (
  (filter (lman, !is.na (next4)) %>% group_by (next4, vulnerable) %>% summarise (n = n()) %>% group_by (vulnerable) %>% mutate (freq = n / sum(n)*100))
  , aes (x=next4, y=freq, fill=factor(vulnerable)))
  + geom_col (position="dodge")
  + ylim (0, 70)
  + xlab ("")
  + ylab ("% of elected deputies")
  + scale_fill_manual (values=movie, labels=c (
    str_c ("not vulnerable\n(", 100-round (mean (filter (lman, !is.na (next4))$vulnerable)*100, 0), "% of sample)")
    , str_c ("vulnerable (last elected in list)\n(", round (mean (filter (lman, !is.na (next4))$vulnerable)*100, 0), "% of sample)")))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-15,-9,-9,-9)))


## (2.2.3) by previous experience ####
filter (lman, !is.na (next4)) %>% group_by (next4, any.b) %>% summarise (n = n()) %>% group_by (any.b) %>% mutate (freq = n / sum(n)*100)

(pNext4_experience <- ggplot (
  (filter (lman, !is.na (next4)) %>% group_by (next4, any.b) %>% summarise (n = n()) %>% group_by (any.b) %>% mutate (freq = n / sum(n)*100))
  , aes (x=next4, y=freq, fill=factor(1-any.b)))
  + geom_col (position="dodge")
  + ylim (0, 70)
  + xlab ("")
  + ylab ("% of elected deputies")
  + scale_fill_manual (values=movie, labels=c (
    str_c ("previous executive\nor legislative experience\n(", round (mean (filter (lman, !is.na (next4))$any.b)*100, 0), "% of sample)")
    , str_c ("no previous experience\n(", 100-round (mean (filter (lman, !is.na (next4))$any.b)*100, 0), "% of sample)")))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-15,-9,-9,-9)))


## (2.2.4) by committee chairmanship ####
filter (lman, !is.na (next4)) %>% group_by (next4, chair_dum) %>% summarise (n = n()) %>% group_by (chair_dum) %>% mutate (freq = n / sum(n)*100)

(pNext4_chair <- ggplot (
  (filter (lman, !is.na (next4)) %>% group_by (next4, chair_dum) %>% summarise (n = n()) %>% group_by (chair_dum) %>% mutate (freq = n / sum(n)*100))
  , aes (x=next4, y=freq, fill=factor(1-chair_dum)))
  + geom_col (position="dodge")
  + ylim (0, 70)
  + xlab ("")
  + ylab ("% of elected deputies")
  + scale_fill_manual (values=movie, labels=c (
    str_c ("committee chair\n(", round (mean (filter (lman, !is.na (next4))$chair_dum)*100, 0), "% of sample)")
    , str_c ("no committee chairmanship\n(", 100-round (mean (filter (lman, !is.na (next4))$chair_dum)*100, 0), "% of sample)")))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-15,-9,-9,-9)))


## (2.2.5) by year of election: midterm vs. concurrent ####
filter (lman, !is.na (next4)) %>% group_by (next4, midterm) %>% summarise (n = n()) %>% group_by (midterm) %>% mutate (freq = n / sum(n)*100)

(pNext4_midterm <- ggplot (
  (filter (lman, !is.na (next4)) %>% group_by (next4, midterm) %>% summarise (n = n()) %>% group_by (midterm) %>% mutate (freq = n / sum(n)*100))
  , aes (x=next4, y=freq, fill=factor(1-midterm)))
  + geom_col (position="dodge")
  + ylim (0, 70)
  + xlab ("")
  + ylab ("% of elected deputies")
  + scale_fill_manual (values=movie, labels=c (
    str_c ("elected in midterm year\n(", round (mean (filter (lman, !is.na (next4))$midterm)*100, 0), "% of sample)")
    , str_c ("elected in concurrent year\n(", 100-round (mean (filter (lman, !is.na (next4))$midterm)*100, 0), "% of sample)")))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-15,-9,-9,-9)))


## (2.2.6) by political party ####
filter (lman, !is.na (next4)) %>% group_by (next4, party) %>% summarise (n = n()) %>% group_by (party) %>% mutate (freq = n / sum(n)*100)

(pNext4_party <- ggplot (
  (filter (lman, !is.na (next4)) %>% group_by (next4, party) %>% summarise (n = n()) %>% group_by (party) %>% mutate (freq = n / sum(n)*100))
  , aes (x=next4, y=freq, fill=party))
  + geom_col (position="dodge")
  + ylim (0, 70)
  + xlab ("")
  + ylab ("% of elected deputies")
  + scale_fill_manual (values=movie, labels=c (
      str_c ("PJ\n(", round (nrow (filter (lman, party=="PJ" & !is.na (next4)))/nrow (filter (lman, !is.na (next4)))*100, 0), "% of sample)")
      , str_c ("UCR\n(", round (nrow (filter (lman, party=="UCR" & !is.na (next4)))/nrow (filter (lman, !is.na (next4)))*100, 0), "% of sample)")
      , str_c ("provincial 3rd party\n(", round (nrow (filter (lman, party=="3rd party" & !is.na (next4)))/nrow (filter (lman, !is.na (next4)))*100, 0), "% of sample)")
      , str_c ("other\n(", round (nrow (filter (lman, party=="other" & !is.na (next4)))/nrow (filter (lman, !is.na (next4)))*100, 0), "% of sample)")))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-15,-9,-9,-9)))


## (2.2.7) by gender ####
filter (lman, !is.na (next4)) %>% group_by (next4, female) %>% summarise (n = n()) %>% group_by (female) %>% mutate (freq = n / sum(n)*100)

(pNext4_gender <- ggplot (
  (filter (lman, !is.na (next4)) %>% group_by (next4, female) %>% summarise (n = n()) %>% group_by (female) %>% mutate (freq = n / sum(n)*100))
  , aes (x=next4, y=freq, fill=factor(female)))
  + geom_col (position="dodge")
  + ylim (0, 70)
  + xlab ("")
  + ylab ("% of elected deputies")
  + scale_fill_manual (values=movie, labels=c (
    str_c ("men\n(", 100-round (mean (filter (lman, !is.na (next4))$female)*100, 0), "% of sample)")
    , str_c ("women\n(", round (mean (filter (lman, !is.na (next4))$female)*100, 0), "% of sample)")))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-15,-9,-9,-9)))




#### (2.3) Consecutive (elected) positions Argentine politicians occupy ####

## preparing the data
cars2 <- cars %>% filter (office != 'convnac' ## compatible w/other elected positions
                         & office != 'minnac' ## not elected
                         & office != 'dipmer' ## not very relevant
                         & !(office == 'sennac' & start < 2001) ## not elected
                         & interino == 0 & assumed == 1) %>% mutate (
                           name2 = ifelse (diffProv == 0, name2, str_c (name2, provincia, sep=' ')) ## to identify these as distinct individuals
                           , office2 = ifelse (office %in% c ('presid', 'vicepresid'), 'presid', ifelse (
                             office %in% c ('gober', 'vicegober'), 'gober', office))
                           , end = ifelse (office2 == 'sennac', end, ifelse (
                             office2 == 'presid' & start < 1995, start + 6, start + 4))
                           , provincia = factor (provincia)
                           , period = factor (period)
                           , name2 = factor (name2)
                           , office = factor (office)
                           , office2 = factor (office2)
                           , interino =  NULL
                           , assumed =  NULL
                           , days = NULL
                           , pWeight = NULL
                           , rest =  NULL) %>% arrange (name2, start) %>% mutate (
                             
                             ## next step: check what happens with the row BELOW:
                             name_office = str_c (name2, office2, sep='_')
                             , name_office_lag = lag (name_office, n=1)
                             , end_lag = lag (end, n=1)
                             , name_office_lead = lead (name_office, n=1)
                             , start_lead = lead (start, n=1)
                             
                             
                             ## we then pass on the data to do the cut. Specifically, we want to identify instances when: (i) next name in list is not consecutive (i.e., different individual-office, or same individual office but far back in time); or (ii) next one IS consecutive, but previous one was not
                             , cutpoint = ifelse (
                               name_office == name_office_lead & start_lead - end <= 0
                               & (name_office != name_office_lag | start - end_lag > 0), 1, 0)
                             , cutpoint = ifelse (is.na (cutpoint), 1, cutpoint)
                             , cutpoint_lag = lag (cutpoint, n=1)
                             , cutpoint2 = ifelse (cutpoint == 1, 1, ifelse (
                               cutpoint_lag == 1, 0, ifelse (
                                 (name_office != name_office_lag) | start - end_lag > 0, 1, 0))) )


## splitting and creating the factors we need
cars2 <- split (cars2, cumsum (cars2$cutpoint2 == 1))
cars2 <- bind_rows (cars2, .id="id") %>% 
  group_by (id) %>% mutate (
    start_min = min.na (start)
    , end_max = max.na (end)
    , n_periods = n()
    ) %>% ungroup () %>% mutate (
      id = str_c (name2, ' -- ', office2, ' -- ', start_min, '-', end_max, sep='')
      , office_type = ifelse (office2 %in% c ('dipnac', 'sennac'), 'legislative', 'executive')
      , office3 = ifelse (office2 == 'dipnac', 'national\ndeputy', ifelse (
        office2 == 'sennac', 'national\nsenator', ifelse (
          office2 == 'mayor', 'mayor', '(vice-)president or\n(vice-)governor')))
      , office_type = factor (office_type)
      , office3 = factor (office3, levels=c ('national\ndeputy', 'national\nsenator', '(vice-)president or\n(vice-)governor', 'mayor'))
      , name_office = NULL
      , name_office_lag = NULL
      , end_lag = NULL
      , name_office_lead = NULL
      , start_lead = NULL
      , cutpoint = NULL
      , cutpoint_lag = NULL
      , name_office = NULL)
summary (cars2)


## calculating the frequencies we'll plot
prop.table (with (unique (select (cars2, id, office3, n_periods)), table (office3, n_periods)), 1)*100
cars3 <- as_tibble (prop.table (with (unique (select (cars2, id, office3, n_periods)), table (office3, n_periods)), 1)*100) %>% mutate (
  office3 = factor (office3, levels=c ('mayor', '(vice-)president or\n(vice-)governor', 'national\nsenator', 'national\ndeputy')))


## plotting the frequencies of interest
(pOffices <- ggplot (cars3, aes (
  x=-as.numeric (n_periods), y=n, fill=office3))
  + geom_bar (stat='identity', position='dodge')
  + coord_flip ()
  + xlab ("spell length (in number of terms)")
  + ylab ("% of spells (normalized by total number of spells per office)")
  + scale_x_continuous (breaks=-1:-9, labels=1:9)
  + scale_y_continuous (breaks=seq (0, 90, by=10))
  + scale_fill_manual (name="", values=movie)
  + theme_classic (base_size=cex_base)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)))




#### (2.4) Resignations over time ####

## (2.4.1) by year of election ####

# resignations and deaths per election year & stacking
lman2 <- filter (lman, elected==1 & assumed==1) %>% mutate (
  elYear = as.numeric (str_split_fixed (as.character (period), '-', 2)[,1])
  ) %>% group_by (elYear) %>% summarise (
    resigned = mean (resignation)*100
    , died = mean (death)*100) %>% pivot_longer (cols=c ("resigned", "died"), names_to="outcome", values_to="value") %>% mutate (
      label = ifelse (elYear %in% c (1993, 2009), "t+2: = party,\n= person", ifelse (elYear %in% c (2001, 2005), "t+2: = party,\n=/= person", ifelse (
  elYear %in% c (1987, 1997, 1999, 2013), "t+2: =/= party,\n=/= person", ""))))

# drawing the plot
(pTime_overall <- ggplot (filter (lman2, outcome=="resigned"), aes (
  x=elYear, y=value, group=outcome))
  + geom_vline (xintercept=seq (1985, 2013, by=4), size=0.5, linetype=2, color="gray75")  ## midterm years
  + geom_line (size=0.8, color=movie[2])
  + geom_label_repel (aes (label=label, color=label), size=1.25)
  + ylim (0, 30)
  + xlab ("year elected")
  + ylab ("% resigned")
  + scale_x_continuous (breaks=seq (1985, 2013, by=4))
  + scale_color_manual (name="", values=c ("white", movie_labels[5], movie_labels[4], movie_labels[3]))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="none", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)))


## (2.4.2) within legislative period

# resignations and deaths per month & stacking
lmonth2 <- filter (lmonth, elected==1 & assumed==1) %>% 
  group_by (month_id_period, midterm) %>% summarise (
    resigned = mean (resignation_m)*100) %>% pivot_longer (cols="resigned", names_to="outcome", values_to="value")

# drawing the plot
(pTime_within <- ggplot (lmonth2, aes (
  x=month_id_period, y=value, color=factor (midterm), linetype=factor (midterm)))
  + geom_line (size=0.65)
  + ylim (0, 8)
  + xlab ("month within legislative term")
  + ylab ("% resigned")
  + scale_x_continuous (breaks=seq (0, 48, by=6))
  + scale_color_manual (values=movie, labels=c ("elected in concurrent year", "elected in midterm year"))
  + scale_linetype_discrete (labels=c ("elected in concurrent year", "elected in midterm year"))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)))


## (2.2.3) distribution of treatment status, within legislative period

# alternations per kind per month & stacking
lmonth2 <- filter (lmonth, elected==1 & assumed==1) %>%
  group_by (month_id_period, midterm) %>% summarise (
    alternation30_nat_spsp = mean (alternation30_nat_spsp)*100
    , alternation30_nat_spdp = mean (alternation30_nat_spdp)*100
    , alternation30_nat_dpdp = mean (alternation30_nat_dpdp)*100
    , alternation30_prov_spsp = mean (alternation30_prov_spsp)*100
    , alternation30_prov_spdp = mean (alternation30_prov_spdp)*100
    , alternation30_prov_dpdp = mean (alternation30_prov_dpdp)*100
  ) %>% pivot_longer (alternation30_nat_spsp:alternation30_prov_dpdp, names_to='treat') %>% mutate (
    level = str_split_fixed (as.character (treat), '_', 3)[,2]
    , level = ifelse (level == 'nat', 'alternation at the\nnational level', 'alternation at the\nprovincial level')
    , level = factor (level, levels=c ('alternation at the\nnational level', 'alternation at the\nprovincial level'))
    , type = str_split_fixed (as.character (treat), '_', 3)[,3]
    , type = ifelse (type == 'spsp', 'same party,\nsame person', ifelse (
      type == 'spdp', 'same party,\ndifferent person', 'different party,\ndifferent person'))
    , type = factor (type, levels=c ('same party,\nsame person', 'same party,\ndifferent person', 'different party,\ndifferent person')) )
summary (lmonth2)

# drawing the plot
(pTreat_within <- ggplot (lmonth2, aes (
  x=month_id_period, y=value, color=factor (midterm), linetype=factor (midterm)))
  + geom_line (size=0.65)
  + ylim (0, 50)
  + xlab ("month within legislative term")
  + ylab ("% legislators exposed to treatment (avg.)")
  + facet_grid (type ~ level)
  + scale_x_continuous (breaks=seq (0, 48, by=6))
  + scale_color_manual (values=movie, labels=c ("elected in concurrent year", "elected in midterm year"))
  + scale_linetype_discrete (labels=c ("elected in concurrent year", "elected in midterm year"))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)))




#### (2.5) Pr(resignation), conditional on alternation in executive position(s) ####

## (2.5.1) doing the calculation (legislator-day dataset: 30-day window)

## stacking the variables of interest (= changing into "long" format)
lday3 <- filter (lday2, elected==1 & assumed==1) %>%
  select (id, name, district, begDate, date, end, resignation, death, position, positionOrd, positionRel, vulnerable, chair, chairFirst, chairSecond, exe.b, leg.b, any.b, midterm, female, party, officeNext_full, officeNext_national, officeNext_subnational, officeNext_elected, officeNext_appointed, alternation30_prov, alternation30_nat) %>% mutate (
    days_since_beg = as.numeric (date - begDate)
    , exclude30 = ifelse (days_since_beg <= 30, 1, 0)
    , exclude60 = ifelse (days_since_beg <= 60, 1, 0)
    , exclude90 = ifelse (days_since_beg <= 90, 1, 0)
    ) %>% pivot_longer (cols=c ("alternation30_prov", "alternation30_nat"), names_to="sample", values_to="alternation30") %>% mutate (
    sample = ifelse (sample=="alternation30_nat", "Alternation: National", "Alternation: Provincial")
    , sample = factor (sample)
    , party = factor (party, levels=c ("PJ", "UCR", "3rd party", "other"))
    , officeNext_natele = ifelse (resignation==1 & officeNext_full=="national-elected", 1, 0)
    , officeNext_natapp = ifelse (resignation==1 & officeNext_full=="national-appointed", 1, 0)
    , officeNext_subele = ifelse (resignation==1 & officeNext_full=="subnational-elected", 1, 0)
    , officeNext_subapp = ifelse (resignation==1 & officeNext_full=="subnational-appointed", 1, 0)
    , officeNext_none = ifelse (resignation==1 & officeNext_full=="none", 1, 0))
summary (lday3)  ## all's well


## calculating Pr(resignation)

# baseline
(lday30_all <- filter (lday3, exclude30 == 0) %>% group_by (sample, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))

(lday30_all02 <- filter (lday3, exclude30 == 0) %>% group_by (sample, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , officeNext_natele = round (mean (officeNext_natele)*100, 3)
  , officeNext_natapp = round (mean (officeNext_natapp)*100, 3)
  , officeNext_subele = round (mean (officeNext_subele)*100, 3)
  , officeNext_subapp = round (mean (officeNext_subapp)*100, 3)))

# position in party list
(lday30_list <- filter (lday3, exclude30 == 0) %>% group_by (sample, positionOrd, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))

# electoral vulnerability
(lday30_vulnerable <- filter (lday3, exclude30 == 0) %>% group_by (sample, vulnerable, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))

# previous experience
(lday30_experience <- filter (lday3, exclude30 == 0) %>% group_by (sample, any.b, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))

# committee chairmanship
(lday30_chair <- filter (lday3, exclude30 == 0) %>% group_by (sample, chair, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))

# midterm vs. concurrent
(lday30_midterm <- filter (lday3, exclude30 == 0) %>% group_by (sample, midterm, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))

# political party
(lday30_party <- filter (lday3, exclude30 == 0) %>% group_by (sample, party, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))

# gender
(lday30_gender <- filter (lday3, exclude30 == 0) %>% group_by (sample, female, alternation30) %>% summarise (
  N = n()
  , end = round (mean (end)*100, 3)
  , resignation = round (mean (resignation)*100, 3)
  , death = round (mean (death)*100, 3)))


## (2.5.2) Drawing the plots

## by position in party list
(pResign_list <- ggplot (lday30_list, aes (
  x=alternation30, y=resignation, fill=positionOrd))
  + geom_col (position="dodge")
  + facet_grid (~ sample)
  + xlab ("") + ylab ("% resigning on a given day")
  + ylim (0, 0.305)
  + scale_fill_manual (values=movie, labels=c ("placed 1st in party list", "placed 2nd in party list", "placed 3rd or lower\nin party list"))
  + theme_classic (base_size=cex_base*.70)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-21,-9,-9,-9)))


## by electoral vulnerability
(pResign_vulnerable <- ggplot (lday30_vulnerable, aes (
  x=alternation30, y=resignation, fill=factor(vulnerable)))
  + geom_col (position="dodge")
  + facet_grid (~ sample)
  + xlab ("") + ylab ("% resigning on a given day")
  + ylim (0, 0.305)
  + scale_fill_manual (values=movie, labels=c ("not vulnerable", "vulnerable"))
  + theme_classic (base_size=cex_base*.70)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-21,-9,-9,-9)))


## by previous experience
(pResign_experience <- ggplot (lday30_experience, aes (
  x=alternation30, y=resignation, fill=factor(1-any.b)))
  + geom_col (position="dodge")
  + facet_grid (~ sample)
  + xlab ("") + ylab ("% resigning on a given day")
  + ylim (0, 0.305)
  + scale_fill_manual (values=movie, labels=c ("previous executive\nor legislative experience", "no previous experience"))
  + theme_classic (base_size=cex_base*.70)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-21,-9,-9,-9)))


## by committee chairmanship
(pResign_chair <- ggplot (lday30_chair, aes (
  x=alternation30, y=resignation, fill=factor(1-chair)))
  + geom_col (position="dodge")
  + facet_grid (~ sample)
  + xlab ("") + ylab ("% resigning on a given day")
  + ylim (0, 0.305)
  + scale_fill_manual (values=movie, labels=c ("committee chair", "does not lead\nany committee"))
  + theme_classic (base_size=cex_base*.70)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-21,-9,-9,-9)))


## by election type: midterm vs. concurrent
(pResign_midterm <- ggplot (lday30_midterm, aes (
  x=alternation30, y=resignation, fill=factor(1-midterm)))
  + geom_col (position="dodge")
  + facet_grid (~ sample)
  + xlab ("") + ylab ("% resigning on a given day")
  + ylim (0, 0.305)
  + scale_fill_manual (values=movie, labels=c ("elected in midterm year", "elected in concurrent year"))
  + theme_classic (base_size=cex_base*.70)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-21,-9,-9,-9)))


## by party ID
(pResign_party <- ggplot (lday30_party, aes (
  x=alternation30, y=resignation, fill=party))
  + geom_col (position="dodge")
  + facet_grid (~ sample)
  + xlab ("") + ylab ("% resigning on a given day")
  + ylim (0, 0.305)
  + scale_fill_manual (values=movie)
  + theme_classic (base_size=cex_base*.70)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-21,-9,-9,-9)))


## by gender
(pResign_gender <- ggplot (lday30_gender, aes (
  x=alternation30, y=resignation, fill=factor(female)))
  + geom_col (position="dodge")
  + facet_grid (~ sample)
  + xlab ("") + ylab ("% resigning on a given day")
  + ylim (0, 0.305)
  + scale_fill_manual (values=movie, labels=c ("men", "women"))
  + theme_classic (base_size=cex_base*.70)
  + theme (legend.position="right", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-21,-9,-9,-9)))


## removing the objects we won't need anymore
rm (cars, cars2, cars3, lday2, lday3)



#### (2.6) Exporting the plots ####

## setting a new WD
setwd ("/Users/adrianlucardi/Dropbox/Current Projects/Resignation/paper/Paper Resignations 10 - LSQ final/Figures/")


## (2.6.1) What happens in 4 years

pwid <- 5
phei <- 1.562

ggsave ("figNext4_list.png"
        , pNext4_list, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figNext4_vulnerable.png"
        , pNext4_vulnerable, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figNext4_experience.png"
        , pNext4_experience, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figNext4_chair.png"
        , pNext4_chair, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figNext4_midterm.png"
        , pNext4_midterm, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figNext4_party.png"
        , pNext4_party, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figNext4_gender.png"
        , pNext4_gender, width=pwid, height=phei, units="in", dpi=600)


## (2.6.2) Evolution over time

pwid <- 5
phei <- 1.965

ggsave ("figTimeOverall.png"
        , pTime_overall, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figTimeWithin.png"
        , pTime_within, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figTreatWithin.png"
        , pTreat_within, width=pwid, height=phei*3, units="in", dpi=600)


## (2.6.3) Career spells

pwid <- 5
phei <- 5.834

ggsave ("figOffices.png"
        , pOffices, width=pwid, height=phei, units="in", dpi=600)


## (2.6.4) Pr(resignation), conditional on executive alternation

pwid <- 6
phei <- 1.8

ggsave ("figResign_list.png"
        , pResign_list, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figResign_vulnerable.png"
        , pResign_vulnerable, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figResign_experience.png"
        , pResign_experience, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figResign_chair.png"
        , pResign_chair, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figResign_midterm.png"
        , pResign_midterm, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figResign_party.png"
        , pResign_party, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figResign_gender.png"
        , pResign_gender, width=pwid, height=phei, units="in", dpi=600)


## putting the original working directory back
setwd (home)




#### (2.7) Table with descriptive statistics ####

## mandate-level data
lman <- lman %>% mutate (
  partyPJ = ifelse (party=="PJ", 1, 0)
  , partyUCR = ifelse (party=="UCR", 1, 0)
  , party3rd = ifelse (party=="3rd party", 1, 0)
  , partyOther = ifelse (party=="other", 1, 0)
  , alternation30_nat_spsp = NA
  , alternation30_nat_spdp = NA
  , alternation30_nat_dpdp = NA
  , alternation30_prov_spsp = NA
  , alternation30_prov_spdp = NA
  , alternation30_prov_dpdp = NA )
(lman_long <- pivot_longer (select (
  filter (lman, elected==1 & assumed==1)
  , name, period
  , resignation, officeNext_elected, officeNext_appointed
  , alternation30_nat_spsp, alternation30_nat_spdp, alternation30_nat_dpdp
  , alternation30_prov_spsp, alternation30_prov_spdp, alternation30_prov_dpdp
  , top01, vulnerable, any.b, chair, midterm, partyPJ, partyUCR, party3rd, partyOther, female
  , incPresi, incGober, voteSh, seats
  , position, positionRel, presid.b, gober.b, mayor.b, sennac.b, dipnac.b, legoth.b
  ), -c (name, period), names_to="varname", values_to="value") %>% mutate (
  varname = factor (varname, levels=c ("resignation", "officeNext_elected", "officeNext_appointed", "alternation30_nat_spsp", "alternation30_nat_spdp", "alternation30_nat_dpdp", "alternation30_prov_spsp", "alternation30_prov_spdp", "alternation30_prov_dpdp", "top01", "vulnerable", "any.b", "chair", "midterm", "partyPJ", "partyUCR", "party3rd", "partyOther", "female", "incPresi", "incGober", "voteSh", "seats", "position", "positionRel", "presid.b", "gober.b", "mayor.b", "sennac.b", "dipnac.b", "legoth.b"))) %>% 
  group_by (varname) %>% summarise (
    N = n ()
    , mean = mean (value) ## yes, we DO want this to get NA's for the variables we don't have
    , sd = sd (value, na.rm=T)
    , min.na = min (value)
    , max.na = max (value)))


## month-level data
lmonth <- lmonth %>% mutate (
  partyPJ = ifelse (party=="PJ", 1, 0)
  , partyUCR = ifelse (party=="UCR", 1, 0)
  , party3rd = ifelse (party=="3rd party", 1, 0)
  , partyOther = ifelse (party=="other", 1, 0)
  , resignation_m100 = resignation_m*100
  , officeNext_elected_m100 = resignation_m*officeNext_elected*100
  , officeNext_appointed_m100 = resignation_m*officeNext_appointed*100 )
(lmonth_long <- pivot_longer (select (
  filter (lmonth, month_id_period >= 2 & elected==1 & assumed==1)
  , name, period, month_id
  , resignation_m100, officeNext_elected_m100, officeNext_appointed_m100
  , alternation30_nat_spsp, alternation30_nat_spdp, alternation30_nat_dpdp
  , alternation30_prov_spsp, alternation30_prov_spdp, alternation30_prov_dpdp
  , top01, positionRel, vulnerable, any.b, chair, midterm, partyPJ, partyUCR, party3rd, partyOther, female
  , incPresi, incGober, voteSh, seats
  , position, presid.b, gober.b, mayor.b, sennac.b, dipnac.b, legoth.b
), -c (name, period, month_id), names_to="varname", values_to="value") %>% mutate (
  varname = factor (varname, levels=c ("resignation_m100", "officeNext_elected_m100", "officeNext_appointed_m100", "alternation30_nat_spsp", "alternation30_nat_spdp", "alternation30_nat_dpdp", "alternation30_prov_spsp", "alternation30_prov_spdp", "alternation30_prov_dpdp", "top01", "vulnerable", "any.b", "chair", "midterm", "partyPJ", "partyUCR", "party3rd", "partyOther", "female", "incPresi", "incGober", "voteSh", "seats", "position", "positionRel", "presid.b", "gober.b", "mayor.b", "sennac.b", "dipnac.b", "legoth.b"))) %>% 
    group_by (varname) %>% summarise (
      N = n()
      , mean = mean (value, na.rm=T)
      , sd = sd (value, na.rm=T)
      , min.na = min (value)
      , max.na = max (value)))


## putting everything together
(tabDescriptive <- cbind (
  as.character (unlist (lman_long[,2]))
  , matrix (sprintf ("%.2f", unlist (round (lman_long[,3:6], 2))), ncol=4)
  , rep ("", nrow (lman_long)), as.character (unlist (lmonth_long[,2]))
  , matrix (sprintf ("%.2f", unlist (round (lmonth_long[,3:6], 2))), ncol=4)))
tabDescriptive[tabDescriptive=="NA"] <- ""
tabDescriptive[4:9,1] <- ""
tabDescriptive

rows <- c (
  "\\emph{Resignation} (0/1)$^{\\dagger}$"
  , "\\emph{Resignation \\& election} (0/1)$^{\\dagger}$"
  , "\\emph{Resignation \\& appointment} (0/1)$^{\\dagger}$"
  
  , "[1.05ex] National (30-day): $=$ \\emph{party,} $=$ \\emph{person} (0:1)"
  , "National (30-day): $=$ \\emph{party,} $\\neq$ \\emph{person} (0:1)"
  , "National (30-day): $\\neq$ \\emph{party,} $\\neq$ \\emph{person} (0:1)"
  , "Provincial (30-day): $=$ \\emph{party,} $=$ \\emph{person} (0:1)"
  , "Provincial (30-day): $=$ \\emph{party,} $\\neq$ \\emph{person} (0:1)"
  , "Provincial (30-day): $\\neq$ \\emph{party,} $\\neq$ \\emph{person} (0:1)"
  
  , "[1.05ex] \\emph{1st in party list} (0/1)"
  , "\\emph{Relative position in list} (0:1)"
  , "\\emph{Electorally vulnerable} (0/1)"
  , "\\emph{Previous experience} (0/1)"
  , "\\emph{Committee chair} (0/1)"
  , "\\emph{Elected in midterm} (0/1)"
  , "\\emph{party: \\textsc{pj}} (0/1)"
  , "\\emph{party: \\textsc{ucr}} (0/1)"
  , "\\emph{party: provincial 3rd party} (0/1)"
  , "\\emph{party: other} (0/1)"
  , "\\emph{Female} (0/1)"
  
  , "[1.05ex] \\emph{Copartisan president} (0/1)"
  , "\\emph{Copartisan governor} (0/1)"
  , "\\emph{Vote share} (0:1)"
  , "\\emph{Seats captured} (\\#)"
  , "\\emph{Position in list} (\\#)"
  , "\\emph{Former (vice-)president} (0/1)"
  , "\\emph{Former (deputy-)governor} (0/1)"
  , "\\emph{Former mayor} (0/1)"
  , "\\emph{Former national senator} (0/1)"
  , "\\emph{Former national deputy} (0/1)"
  , "\\emph{Former (supra-)national legislator} (0/1)")

Header1 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{(a) By legislative mandate}} & & \\multicolumn{5}{c}{\\textbf{(b) By legislator-month$^{*}$}} \\\\ \\cmidrule{2-6} \\cmidrule{8-12} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{$N$} & \\multicolumn{1}{c}{mean} & \\multicolumn{1}{c}{sd.} & \\multicolumn{1}{c}{min.} & \\multicolumn{1}{c}{max.} & & \\multicolumn{1}{c}{$N$} & \\multicolumn{1}{c}{mean} & \\multicolumn{1}{c}{sd.} & \\multicolumn{1}{c}{min.} & \\multicolumn{1}{c}{max.} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$command <- c (Header1, Header2)
print (xtable ( cbind (rows, tabDescriptive)
                , align=c ('l','l','r','r','r','r','r','c','r','r','r','r','r')
                , digits=digits
                , caption="\\emph{Descriptive statistics}"
                , label="T:descriptive")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )





#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 3 // STATISTICAL MODELS ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#


###### (3.1) Writing the formula(s) we'll use ######
## to save space; and
## to quickly implement corrections, if needed


## (3.1.1) Main variables

# (3.1.1.1) 30-day window, main formula, no controls
fm01_main30 <- "alternation30_nat_spsp + alternation30_nat_spdp + alternation30_nat_dpdp + alternation30_prov_spsp + alternation30_prov_spdp + alternation30_prov_dpdp"

# (3.1.1.2) 30-day window, including interactions
fm01_position30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*positionRel) + alternation30_nat_spdp + I(alternation30_nat_spdp*positionRel) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*positionRel) + alternation30_prov_spsp + I(alternation30_prov_spsp*positionRel) + alternation30_prov_spdp + I(alternation30_prov_spdp*positionRel) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*positionRel) + positionRel"

fm01_vulnerable30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*vulnerable) + alternation30_nat_spdp + I(alternation30_nat_spdp*vulnerable) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*vulnerable) + alternation30_prov_spsp + I(alternation30_prov_spsp*vulnerable) + alternation30_prov_spdp + I(alternation30_prov_spdp*vulnerable) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*vulnerable) + vulnerable"

fm01_exper30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*any.b) + alternation30_nat_spdp + I(alternation30_nat_spdp*any.b) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*any.b) + alternation30_prov_spsp + I(alternation30_prov_spsp*any.b) + alternation30_prov_spdp + I(alternation30_prov_spdp*any.b) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*any.b) + any.b"

fm01_chair30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*chair) + alternation30_nat_spdp + I(alternation30_nat_spdp*chair) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*chair) + alternation30_prov_spsp + I(alternation30_prov_spsp*chair) + alternation30_prov_spdp + I(alternation30_prov_spdp*chair) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*chair) + chair"

fm01_midterm30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*midterm) + alternation30_nat_spdp + I(alternation30_nat_spdp*midterm) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*midterm) + alternation30_prov_spsp + I(alternation30_prov_spsp*midterm) + alternation30_prov_spdp + I(alternation30_prov_spdp*midterm) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*midterm) + midterm"

fm01_gender30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*female) + alternation30_nat_spdp + I(alternation30_nat_spdp*female) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*female) + alternation30_prov_spsp + I(alternation30_prov_spsp*female) + alternation30_prov_spdp + I(alternation30_prov_spdp*female) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*female) + female"

fm01_party30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*partyPJ) + I(alternation30_nat_spsp*partyOther) + I(alternation30_nat_spsp*party3rd) + alternation30_nat_spdp + I(alternation30_nat_spdp*partyPJ) + I(alternation30_nat_spdp*partyOther) + I(alternation30_nat_spdp*party3rd) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*partyPJ) + I(alternation30_nat_dpdp*partyOther) + I(alternation30_nat_dpdp*party3rd) + alternation30_prov_spsp + I(alternation30_prov_spsp*partyPJ) + I(alternation30_prov_spsp*partyOther) + I(alternation30_prov_spsp*party3rd) + alternation30_prov_spdp + I(alternation30_prov_spdp*partyPJ) + I(alternation30_prov_spdp*partyOther) + I(alternation30_prov_spdp*party3rd) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*partyPJ) + I(alternation30_prov_dpdp*partyOther) + I(alternation30_prov_dpdp*party3rd) + partyPJ + partyUCR + party3rd" ## baseline is UCR

# (3.1.1.3) 60-day window, main formula, no controls
fm01_main60 <- "alternation60_nat_spsp + alternation60_nat_spdp + alternation60_nat_dpdp + alternation60_prov_spsp + alternation60_prov_spdp + alternation60_prov_dpdp"

# (3.1.1.4) 60-day window, including interactions
fm01_position60 <- "alternation60_nat_spsp + I(alternation60_nat_spsp*positionRel) + alternation60_nat_spdp + I(alternation60_nat_spdp*positionRel) + alternation60_nat_dpdp + I(alternation60_nat_dpdp*positionRel) + alternation60_prov_spsp + I(alternation60_prov_spsp*positionRel) + alternation60_prov_spdp + I(alternation60_prov_spdp*positionRel) + alternation60_prov_dpdp + I(alternation60_prov_dpdp*positionRel) + positionRel"

fm01_vulnerable60 <- "alternation60_nat_spsp + I(alternation60_nat_spsp*vulnerable) + alternation60_nat_spdp + I(alternation60_nat_spdp*vulnerable) + alternation60_nat_dpdp + I(alternation60_nat_dpdp*vulnerable) + alternation60_prov_spsp + I(alternation60_prov_spsp*vulnerable) + alternation60_prov_spdp + I(alternation60_prov_spdp*vulnerable) + alternation60_prov_dpdp + I(alternation60_prov_dpdp*vulnerable) + vulnerable"

fm01_exper60 <- "alternation60_nat_spsp + I(alternation60_nat_spsp*any.b) + alternation60_nat_spdp + I(alternation60_nat_spdp*any.b) + alternation60_nat_dpdp + I(alternation60_nat_dpdp*any.b) + alternation60_prov_spsp + I(alternation60_prov_spsp*any.b) + alternation60_prov_spdp + I(alternation60_prov_spdp*any.b) + alternation60_prov_dpdp + I(alternation60_prov_dpdp*any.b) + any.b"

fm01_chair60 <- "alternation60_nat_spsp + I(alternation60_nat_spsp*chair) + alternation60_nat_spdp + I(alternation60_nat_spdp*chair) + alternation60_nat_dpdp + I(alternation60_nat_dpdp*chair) + alternation60_prov_spsp + I(alternation60_prov_spsp*chair) + alternation60_prov_spdp + I(alternation60_prov_spdp*chair) + alternation60_prov_dpdp + I(alternation60_prov_dpdp*chair) + chair"

fm01_midterm60 <- "alternation60_nat_spsp + I(alternation60_nat_spsp*midterm) + alternation60_nat_spdp + I(alternation60_nat_spdp*midterm) + alternation60_nat_dpdp + I(alternation60_nat_dpdp*midterm) + alternation60_prov_spsp + I(alternation60_prov_spsp*midterm) + alternation60_prov_spdp + I(alternation60_prov_spdp*midterm) + alternation60_prov_dpdp + I(alternation60_prov_dpdp*midterm) + midterm"

fm01_gender60 <- "alternation60_nat_spsp + I(alternation60_nat_spsp*female) + alternation60_nat_spdp + I(alternation60_nat_spdp*female) + alternation60_nat_dpdp + I(alternation60_nat_dpdp*female) + alternation60_prov_spsp + I(alternation60_prov_spsp*female) + alternation60_prov_spdp + I(alternation60_prov_spdp*female) + alternation60_prov_dpdp + I(alternation60_prov_dpdp*female) + female"

fm01_party60 <- "alternation60_nat_spsp + I(alternation60_nat_spsp*partyPJ) + I(alternation60_nat_spsp*partyOther) + I(alternation60_nat_spsp*party3rd) + alternation60_nat_spdp + I(alternation60_nat_spdp*partyPJ) + I(alternation60_nat_spdp*partyOther) + I(alternation60_nat_spdp*party3rd) + alternation60_nat_dpdp + I(alternation60_nat_dpdp*partyPJ) + I(alternation60_nat_dpdp*partyOther) + I(alternation60_nat_dpdp*party3rd) + alternation60_prov_spsp + I(alternation60_prov_spsp*partyPJ) + I(alternation60_prov_spsp*partyOther) + I(alternation60_prov_spsp*party3rd) + alternation60_prov_spdp + I(alternation60_prov_spdp*partyPJ) + I(alternation60_prov_spdp*partyOther) + I(alternation60_prov_spdp*party3rd) + alternation60_prov_dpdp + I(alternation60_prov_dpdp*partyPJ) + I(alternation60_prov_dpdp*partyOther) + I(alternation60_prov_dpdp*party3rd) + partyPJ + partyUCR + party3rd"

# (3.1.1.5) 90-day window, main formula, no controls
fm01_main90 <- "alternation90_nat_spsp + alternation90_nat_spdp + alternation90_nat_dpdp + alternation90_prov_spsp + alternation90_prov_spdp + alternation90_prov_dpdp"

# (3.1.1.6) 90-day window, including interactions
fm01_position90 <- "alternation90_nat_spsp + I(alternation90_nat_spsp*positionRel) + alternation90_nat_spdp + I(alternation90_nat_spdp*positionRel) + alternation90_nat_dpdp + I(alternation90_nat_dpdp*positionRel) + alternation90_prov_spsp + I(alternation90_prov_spsp*positionRel) + alternation90_prov_spdp + I(alternation90_prov_spdp*positionRel) + alternation90_prov_dpdp + I(alternation90_prov_dpdp*positionRel) + positionRel"

fm01_vulnerable90 <- "alternation90_nat_spsp + I(alternation90_nat_spsp*vulnerable) + alternation90_nat_spdp + I(alternation90_nat_spdp*vulnerable) + alternation90_nat_dpdp + I(alternation90_nat_dpdp*vulnerable) + alternation90_prov_spsp + I(alternation90_prov_spsp*vulnerable) + alternation90_prov_spdp + I(alternation90_prov_spdp*vulnerable) + alternation90_prov_dpdp + I(alternation90_prov_dpdp*vulnerable) + vulnerable"

fm01_exper90 <- "alternation90_nat_spsp + I(alternation90_nat_spsp*any.b) + alternation90_nat_spdp + I(alternation90_nat_spdp*any.b) + alternation90_nat_dpdp + I(alternation90_nat_dpdp*any.b) + alternation90_prov_spsp + I(alternation90_prov_spsp*any.b) + alternation90_prov_spdp + I(alternation90_prov_spdp*any.b) + alternation90_prov_dpdp + I(alternation90_prov_dpdp*any.b) + any.b"

fm01_chair90 <- "alternation90_nat_spsp + I(alternation90_nat_spsp*chair) + alternation90_nat_spdp + I(alternation90_nat_spdp*chair) + alternation90_nat_dpdp + I(alternation90_nat_dpdp*chair) + alternation90_prov_spsp + I(alternation90_prov_spsp*chair) + alternation90_prov_spdp + I(alternation90_prov_spdp*chair) + alternation90_prov_dpdp + I(alternation90_prov_dpdp*chair) + chair"

fm01_midterm90 <- "alternation90_nat_spsp + I(alternation90_nat_spsp*midterm) + alternation90_nat_spdp + I(alternation90_nat_spdp*midterm) + alternation90_nat_dpdp + I(alternation90_nat_dpdp*midterm) + alternation90_prov_spsp + I(alternation90_prov_spsp*midterm) + alternation90_prov_spdp + I(alternation90_prov_spdp*midterm) + alternation90_prov_dpdp + I(alternation90_prov_dpdp*midterm) + midterm"

fm01_gender90 <- "alternation90_nat_spsp + I(alternation90_nat_spsp*female) + alternation90_nat_spdp + I(alternation90_nat_spdp*female) + alternation90_nat_dpdp + I(alternation90_nat_dpdp*female) + alternation90_prov_spsp + I(alternation90_prov_spsp*female) + alternation90_prov_spdp + I(alternation90_prov_spdp*female) + alternation90_prov_dpdp + I(alternation90_prov_dpdp*female) + female"

fm01_party90 <- "alternation90_nat_spsp + I(alternation90_nat_spsp*partyPJ) + I(alternation90_nat_spsp*partyOther) + I(alternation90_nat_spsp*party3rd) + alternation90_nat_spdp + I(alternation90_nat_spdp*partyPJ) + I(alternation90_nat_spdp*partyOther) + I(alternation90_nat_spdp*party3rd) + alternation90_nat_dpdp + I(alternation90_nat_dpdp*partyPJ) + I(alternation90_nat_dpdp*partyOther) + I(alternation90_nat_dpdp*party3rd) + alternation90_prov_spsp + I(alternation90_prov_spsp*partyPJ) + I(alternation90_prov_spsp*partyOther) + I(alternation90_prov_spsp*party3rd) + alternation90_prov_spdp + I(alternation90_prov_spdp*partyPJ) + I(alternation90_prov_spdp*partyOther) + I(alternation90_prov_spdp*party3rd) + alternation90_prov_dpdp + I(alternation90_prov_dpdp*partyPJ) + I(alternation90_prov_dpdp*partyOther) + I(alternation90_prov_dpdp*party3rd) + partyPJ + partyUCR + party3rd"


## (3.1.2) controls

# (3.1.2.1) all controls
fm02_contall <- "+ female + incGober + incPresi + midterm + voteSh + chair + presid.b + gober.b + sennac.b + dipnac.b + legoth.b + mayor.b + party + log(seats) + positionRel + vulnerable"

# (3.1.2.2) all controls, GLM
fm02_contall_glm <- "+ female + incGober + incPresi + midterm + voteSh + chair + presid.b + gober.b + sennac.b + dipnac.b + legoth.b + mayor.b + party + log(seats) + positionRel + vulnerable + district + period"


## (3.1.3) extensions: clustered SE's, fixed effects, etc

# (3.1.3.1) no FE's, clustered SE's
fm03_clname <- "| 0 | 0 | name"

# (3.1.3.2) control FE's, no individual FE's, clustered SE's
fm03_cfeclname <- "| district + period + month_id_period | 0 | name"

# (3.1.3.3) control FE's, individual FE's, clustered SE's
fm03_ifeclname <- "| name + period + month_id_period | 0 | name"

# (3.1.3.4) legislator-mandate FE's, clustered SE's
fm03_lmfeclname <- "| id + month_id_period | 0 | name"

# (3.1.3.5) duration dummies, for glm's
fm03_durfe <- "+ factor(month_id_period)"

# (3.1.3.6) FE's by treatment (@ provincial level): district-period-agrupacion
fm03_cltreat <- "| 0 | 0 | district-period-agrupacion"



###### (3.2) Month-level analysis ######

### updating the dataset
lmonth3 <- filter (lmonth, elected==1 & assumed==1) %>% mutate (
  resignation_m100 = resignation_m*100
  , officeNext_national100 = officeNext_national*resignation_m100
  , officeNext_subnational100 = officeNext_subnational*resignation_m100
  , officeNext_elected100 = officeNext_elected*resignation_m100
  , officeNext_appointed100 = officeNext_appointed*resignation_m100
  , officeNext_national = officeNext_national*resignation_m
  , officeNext_subnational = officeNext_subnational*resignation_m
  , officeNext_elected = officeNext_elected*resignation_m
  , officeNext_appointed = officeNext_appointed*resignation_m
  )
summary (lmonth3)



#### (3.2.1) 30-day window, OLS specifications ####

## (3.2.1.0) no interactions

# outcome is resignation
(sum_mols30_all10a <- summary (mols30_all10a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2)))) ## no controls
(sum_mols30_all10b <- summary (mols30_all10b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2)))) ## all controls
(sum_mols30_all10c <- summary (mols30_all10c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2)))) ## individual FE's
(sum_mols30_all10d <- summary (mols30_all10d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))  ## legislator-mandate + month ID FE's

# outcome is resignation + elected position
(sum_mols30_all20a <- summary (mols30_all20a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all20b <- summary (mols30_all20b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all20c <- summary (mols30_all20c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all20d <- summary (mols30_all20d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all30a <- summary (mols30_all30a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all30b <- summary (mols30_all30b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all30c <- summary (mols30_all30c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all30d <- summary (mols30_all30d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))


## (3.2.1.1) interacting with top position in party list

# outcome is resignation
(sum_mols30_all11a <- summary (mols30_all11a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all11b <- summary (mols30_all11b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all11c <- summary (mols30_all11c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all11d <- summary (mols30_all11d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_mols30_all21a <- summary (mols30_all21a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all21b <- summary (mols30_all21b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all21c <- summary (mols30_all21c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all21d <- summary (mols30_all21d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all31a <- summary (mols30_all31a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all31b <- summary (mols30_all31b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all31c <- summary (mols30_all31c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all31d <- summary (mols30_all31d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))


## (3.2.1.2) interacting with vulnerability

# outcome is resignation
(sum_mols30_all12a <- summary (mols30_all12a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all12b <- summary (mols30_all12b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all12c <- summary (mols30_all12c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all12d <- summary (mols30_all12d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_mols30_all22a <- summary (mols30_all22a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all22b <- summary (mols30_all22b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all22c <- summary (mols30_all22c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all22d <- summary (mols30_all22d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all32a <- summary (mols30_all32a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all32b <- summary (mols30_all32b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all32c <- summary (mols30_all32c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all32d <- summary (mols30_all32d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))


## (3.2.1.3) interacting with previous experience

# outcome is resignation
(sum_mols30_all13a <- summary (mols30_all13a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all13b <- summary (mols30_all13b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all13c <- summary (mols30_all13c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all13d <- summary (mols30_all13d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_mols30_all23a <- summary (mols30_all23a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all23b <- summary (mols30_all23b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all23c <- summary (mols30_all23c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all23d <- summary (mols30_all23d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all33a <- summary (mols30_all33a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all33b <- summary (mols30_all33b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all33c <- summary (mols30_all33c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all33d <- summary (mols30_all33d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))


## (3.2.1.4) interacting with committee chairmanship

# outcome is resignation
(sum_mols30_all14a <- summary (mols30_all14a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all14b <- summary (mols30_all14b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all14c <- summary (mols30_all14c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all14d <- summary (mols30_all14d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_mols30_all24a <- summary (mols30_all24a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all24b <- summary (mols30_all24b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all24c <- summary (mols30_all24c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all24d <- summary (mols30_all24d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all34a <- summary (mols30_all34a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all34b <- summary (mols30_all34b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all34c <- summary (mols30_all34c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all34d <- summary (mols30_all34d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))


## (3.2.1.5) interacting with election in midterm

# outcome is resignation
(sum_mols30_all15a <- summary (mols30_all15a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all15b <- summary (mols30_all15b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all15c <- summary (mols30_all15c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all15d <- summary (mols30_all15d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_mols30_all25a <- summary (mols30_all25a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all25b <- summary (mols30_all25b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all25c <- summary (mols30_all25c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all25d <- summary (mols30_all25d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all35a <- summary (mols30_all35a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all35b <- summary (mols30_all35b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all35c <- summary (mols30_all35c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all35d <- summary (mols30_all35d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))


## (3.2.1.6) interacting with gender

# outcome is resignation
(sum_mols30_all16a <- summary (mols30_all16a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all16b <- summary (mols30_all16b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all16c <- summary (mols30_all16c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all16d <- summary (mols30_all16d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_mols30_all26a <- summary (mols30_all26a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all26b <- summary (mols30_all26b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all26c <- summary (mols30_all26c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all26d <- summary (mols30_all26d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all36a <- summary (mols30_all36a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all36b <- summary (mols30_all36b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all36c <- summary (mols30_all36c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all36d <- summary (mols30_all36d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))


## (3.2.1.7) interacting with party ID

# outcome is resignation
(sum_mols30_all17a <- summary (mols30_all17a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all17b <- summary (mols30_all17b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all17c <- summary (mols30_all17c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all17d <- summary (mols30_all17d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_mols30_all27a <- summary (mols30_all27a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all27b <- summary (mols30_all27b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all27c <- summary (mols30_all27c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all27d <- summary (mols30_all27d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_mols30_all37a <- summary (mols30_all37a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party30, fm03_clname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all37b <- summary (mols30_all37b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party30, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all37c <- summary (mols30_all37c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party30, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 2))))
(sum_mols30_all37d <- summary (mols30_all37d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party30, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 2))))



#### (3.2.2) 30-day window, survival specifications ####

## (3.2.2.0) no interactions

# outcome is resignation
(sum_surv30_all10a <- summ (surv30_all10a <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_main30, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)
(sum_surv30_all10b <- summ (surv30_all10b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_main30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all20a <- summ (surv30_all20a <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_main30, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)
(sum_surv30_all20b <- summ (surv30_all20b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_main30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all30a <- summ (surv30_all30a <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_main30, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)
(sum_surv30_all30b <- summ (surv30_all30b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_main30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## (3.2.2.1) interacting with top position in party list

# outcome is resignation
(sum_surv30_all11b <- summ (surv30_all11b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_position30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all21b <- summ (surv30_all21b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_position30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all31b <- summ (surv30_all31b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_position30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## (3.2.2.2) interacting with vulnerability

# outcome is resignation
(sum_surv30_all12b <- summ (surv30_all12b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_vulnerable30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all22b <- summ (surv30_all22b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_vulnerable30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all32b <- summ (surv30_all32b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_vulnerable30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## (3.2.2.3) interacting with previous experience

# outcome is resignation
(sum_surv30_all13b <- summ (surv30_all13b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_exper30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all23b <- summ (surv30_all23b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_exper30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all33b <- summ (surv30_all33b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_exper30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## (3.2.2.4) interacting with committee chairmanship

# outcome is resignation
(sum_surv30_all14b <- summ (surv30_all14b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_vulnerable30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all24b <- summ (surv30_all24b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_vulnerable30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all34b <- summ (surv30_all34b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_vulnerable30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## (3.2.2.5) interacting with election in midterm

# outcome is resignation
(sum_surv30_all15b <- summ (surv30_all15b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_midterm30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all25b <- summ (surv30_all25b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_midterm30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all35b <- summ (surv30_all35b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_midterm30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## (3.2.2.6) interacting with gender

# outcome is resignation
(sum_surv30_all16b <- summ (surv30_all16b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_gender30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all26b <- summ (surv30_all26b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_gender30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all36b <- summ (surv30_all36b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_gender30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## (3.2.2.7) interacting with party ID

# outcome is resignation
(sum_surv30_all17b <- summ (surv30_all17b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_party30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv30_all27b <- summ (surv30_all27b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_party30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv30_all37b <- summ (surv30_all37b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_party30, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 2)), robust="HC1", cluster="name")$coeftable)


## removing the models, that take A LOT of space
rm (
  surv30_all10a, surv30_all10b, surv30_all20a, surv30_all20b, surv30_all30a, surv30_all30b, surv30_all40a, surv30_all40b
  , surv30_all11a, surv30_all11b, surv30_all21a, surv30_all21b, surv30_all31a, surv30_all31b, surv30_all41a, surv30_all41b
  , surv30_all12a, surv30_all12b, surv30_all22a, surv30_all22b, surv30_all32a, surv30_all32b, surv30_all42a, surv30_all42b
  , surv30_all13a, surv30_all13b, surv30_all23a, surv30_all23b, surv30_all33a, surv30_all33b, surv30_all43a, surv30_all43b
  , surv30_all14a, surv30_all14b, surv30_all24a, surv30_all24b, surv30_all34a, surv30_all34b, surv30_all44a, surv30_all44b
  , surv30_all15a, surv30_all15b, surv30_all25a, surv30_all25b, surv30_all35a, surv30_all35b, surv30_all45a, surv30_all45b
  , surv30_all16a, surv30_all16b, surv30_all26a, surv30_alxl26b, surv30_all36a, surv30_all36b, surv30_all46a, surv30_all46b
  , surv30_all17a, surv30_all17b, surv30_all27a, surv30_all27b, surv30_all37a, surv30_all37b, surv30_all47a, surv30_all47b)




#### (3.2.3) 60-day window, OLS specifications ####

## (3.2.3.0) no interactions

# outcome is resignation
(sum_mols60_all10a <- summary (mols60_all10a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3)))) ## no controls
(sum_mols60_all10b <- summary (mols60_all10b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3)))) ## all controls
(sum_mols60_all10c <- summary (mols60_all10c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3)))) ## individual FE's
(sum_mols60_all10d <- summary (mols60_all10d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))  ## legislator-mandate + month ID FE's

# outcome is resignation + elected position
(sum_mols60_all20a <- summary (mols60_all20a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all20b <- summary (mols60_all20b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all20c <- summary (mols60_all20c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all20d <- summary (mols60_all20d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all30a <- summary (mols60_all30a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all30b <- summary (mols60_all30b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all30c <- summary (mols60_all30c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all30d <- summary (mols60_all30d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))


## (3.2.3.1) interacting with top position in party list

# outcome is resignation
(sum_mols60_all11a <- summary (mols60_all11a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all11b <- summary (mols60_all11b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all11c <- summary (mols60_all11c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all11d <- summary (mols60_all11d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_mols60_all21a <- summary (mols60_all21a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all21b <- summary (mols60_all21b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all21c <- summary (mols60_all21c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all21d <- summary (mols60_all21d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all31a <- summary (mols60_all31a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all31b <- summary (mols60_all31b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all31c <- summary (mols60_all31c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all31d <- summary (mols60_all31d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))


## (3.2.3.2) interacting with vulnerability

# outcome is resignation
(sum_mols60_all12a <- summary (mols60_all12a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all12b <- summary (mols60_all12b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all12c <- summary (mols60_all12c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all12d <- summary (mols60_all12d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_mols60_all22a <- summary (mols60_all22a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all22b <- summary (mols60_all22b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all22c <- summary (mols60_all22c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all22d <- summary (mols60_all22d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all32a <- summary (mols60_all32a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all32b <- summary (mols60_all32b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all32c <- summary (mols60_all32c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all32d <- summary (mols60_all32d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))


## (3.2.3.3) interacting with previous experience

# outcome is resignation
(sum_mols60_all13a <- summary (mols60_all13a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all13b <- summary (mols60_all13b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all13c <- summary (mols60_all13c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all13d <- summary (mols60_all13d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_mols60_all23a <- summary (mols60_all23a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all23b <- summary (mols60_all23b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all23c <- summary (mols60_all23c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all23d <- summary (mols60_all23d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all33a <- summary (mols60_all33a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all33b <- summary (mols60_all33b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all33c <- summary (mols60_all33c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all33d <- summary (mols60_all33d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))


## (3.2.3.4) interacting with committee chairmanship

# outcome is resignation
(sum_mols60_all14a <- summary (mols60_all14a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all14b <- summary (mols60_all14b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all14c <- summary (mols60_all14c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all14d <- summary (mols60_all14d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_mols60_all24a <- summary (mols60_all24a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all24b <- summary (mols60_all24b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all24c <- summary (mols60_all24c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all24d <- summary (mols60_all24d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all34a <- summary (mols60_all34a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all34b <- summary (mols60_all34b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all34c <- summary (mols60_all34c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all34d <- summary (mols60_all34d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))


## (3.2.3.5) interacting with election in midterm

# outcome is resignation
(sum_mols60_all15a <- summary (mols60_all15a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all15b <- summary (mols60_all15b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all15c <- summary (mols60_all15c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all15d <- summary (mols60_all15d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_mols60_all25a <- summary (mols60_all25a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all25b <- summary (mols60_all25b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all25c <- summary (mols60_all25c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all25d <- summary (mols60_all25d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all35a <- summary (mols60_all35a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all35b <- summary (mols60_all35b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all35c <- summary (mols60_all35c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all35d <- summary (mols60_all35d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))


## (3.2.3.6) interacting with gender

# outcome is resignation
(sum_mols60_all16a <- summary (mols60_all16a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all16b <- summary (mols60_all16b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all16c <- summary (mols60_all16c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all16d <- summary (mols60_all16d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_mols60_all26a <- summary (mols60_all26a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all26b <- summary (mols60_all26b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all26c <- summary (mols60_all26c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all26d <- summary (mols60_all26d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all36a <- summary (mols60_all36a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all36b <- summary (mols60_all36b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all36c <- summary (mols60_all36c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all36d <- summary (mols60_all36d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))


## (3.2.3.7) interacting with party ID

# outcome is resignation
(sum_mols60_all17a <- summary (mols60_all17a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all17b <- summary (mols60_all17b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all17c <- summary (mols60_all17c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all17d <- summary (mols60_all17d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_mols60_all27a <- summary (mols60_all27a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all27b <- summary (mols60_all27b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all27c <- summary (mols60_all27c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all27d <- summary (mols60_all27d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_mols60_all37a <- summary (mols60_all37a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party60, fm03_clname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all37b <- summary (mols60_all37b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party60, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all37c <- summary (mols60_all37c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party60, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 3))))
(sum_mols60_all37d <- summary (mols60_all37d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party60, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 3))))



#### (3.2.4) 60-day window, survival specifications ####

## (3.2.4.0) no interactions

# outcome is resignation
(sum_surv60_all10a <- summ (surv60_all10a <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_main60, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 3)), robust="HC1", cluster="name")$coeftable)
(sum_surv60_all10b <- summ (surv60_all10b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_main60, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 3)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv60_all20a <- summ (surv60_all20a <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_main60, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 3)), robust="HC1", cluster="name")$coeftable)
(sum_surv60_all20b <- summ (surv60_all20b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_main60, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 3)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv60_all30a <- summ (surv60_all30a <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_main60, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 3)), robust="HC1", cluster="name")$coeftable)
(sum_surv60_all30b <- summ (surv60_all30b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_main60, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 3)), robust="HC1", cluster="name")$coeftable)


## removing the models, that take A LOT of space
rm (
  surv60_all10a, surv60_all10b, surv60_all20a, surv60_all20b, surv60_all30a, surv60_all30b)




#### (3.2.5) 90-day window, OLS specifications ####

## (3.2.5.0) no interactions

# outcome is resignation
(sum_mols90_all10a <- summary (mols90_all10a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4)))) ## no controls
(sum_mols90_all10b <- summary (mols90_all10b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4)))) ## all controls
(sum_mols90_all10c <- summary (mols90_all10c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4)))) ## individual FE's
(sum_mols90_all10d <- summary (mols90_all10d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_main90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))  ## legislator-mandate + month ID FE's

# outcome is resignation + elected position
(sum_mols90_all20a <- summary (mols90_all20a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all20b <- summary (mols90_all20b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all20c <- summary (mols90_all20c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all20d <- summary (mols90_all20d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all30a <- summary (mols90_all30a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all30b <- summary (mols90_all30b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all30c <- summary (mols90_all30c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all30d <- summary (mols90_all30d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))


## (3.2.5.1) interacting with top position in party list

# outcome is resignation
(sum_mols90_all11a <- summary (mols90_all11a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all11b <- summary (mols90_all11b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all11c <- summary (mols90_all11c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all11d <- summary (mols90_all11d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_position90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_mols90_all21a <- summary (mols90_all21a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all21b <- summary (mols90_all21b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all21c <- summary (mols90_all21c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all21d <- summary (mols90_all21d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all31a <- summary (mols90_all31a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all31b <- summary (mols90_all31b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all31c <- summary (mols90_all31c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all31d <- summary (mols90_all31d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))


## (3.2.5.2) interacting with vulnerability

# outcome is resignation
(sum_mols90_all12a <- summary (mols90_all12a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all12b <- summary (mols90_all12b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all12c <- summary (mols90_all12c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all12d <- summary (mols90_all12d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_vulnerable90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_mols90_all22a <- summary (mols90_all22a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all22b <- summary (mols90_all22b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all22c <- summary (mols90_all22c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all22d <- summary (mols90_all22d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all32a <- summary (mols90_all32a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all32b <- summary (mols90_all32b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all32c <- summary (mols90_all32c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all32d <- summary (mols90_all32d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))


## (3.2.5.3) interacting with previous experience

# outcome is resignation
(sum_mols90_all13a <- summary (mols90_all13a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all13b <- summary (mols90_all13b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all13c <- summary (mols90_all13c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all13d <- summary (mols90_all13d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_exper90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_mols90_all23a <- summary (mols90_all23a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all23b <- summary (mols90_all23b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all23c <- summary (mols90_all23c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all23d <- summary (mols90_all23d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all33a <- summary (mols90_all33a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all33b <- summary (mols90_all33b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all33c <- summary (mols90_all33c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all33d <- summary (mols90_all33d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))


## (3.2.5.4) interacting with committee chairmanship

# outcome is resignation
(sum_mols90_all14a <- summary (mols90_all14a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all14b <- summary (mols90_all14b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all14c <- summary (mols90_all14c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all14d <- summary (mols90_all14d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_chair90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_mols90_all24a <- summary (mols90_all24a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all24b <- summary (mols90_all24b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all24c <- summary (mols90_all24c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all24d <- summary (mols90_all24d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all34a <- summary (mols90_all34a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all34b <- summary (mols90_all34b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all34c <- summary (mols90_all34c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all34d <- summary (mols90_all34d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))


## (3.2.5.5) interacting with election in midterm

# outcome is resignation
(sum_mols90_all15a <- summary (mols90_all15a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all15b <- summary (mols90_all15b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all15c <- summary (mols90_all15c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all15d <- summary (mols90_all15d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_midterm90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_mols90_all25a <- summary (mols90_all25a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all25b <- summary (mols90_all25b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all25c <- summary (mols90_all25c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all25d <- summary (mols90_all25d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all35a <- summary (mols90_all35a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all35b <- summary (mols90_all35b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all35c <- summary (mols90_all35c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all35d <- summary (mols90_all35d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))


## (3.2.5.6) interacting with gender

# outcome is resignation
(sum_mols90_all16a <- summary (mols90_all16a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all16b <- summary (mols90_all16b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all16c <- summary (mols90_all16c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all16d <- summary (mols90_all16d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_gender90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_mols90_all26a <- summary (mols90_all26a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all26b <- summary (mols90_all26b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all26c <- summary (mols90_all26c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all26d <- summary (mols90_all26d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all36a <- summary (mols90_all36a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all36b <- summary (mols90_all36b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all36c <- summary (mols90_all36c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all36d <- summary (mols90_all36d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))


## (3.2.5.7) interacting with party ID

# outcome is resignation
(sum_mols90_all17a <- summary (mols90_all17a <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all17b <- summary (mols90_all17b <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all17c <- summary (mols90_all17c <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all17d <- summary (mols90_all17d <- felm (
  as.formula (paste0 ("resignation_m100", " ~ ", fm01_party90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_mols90_all27a <- summary (mols90_all27a <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all27b <- summary (mols90_all27b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all27c <- summary (mols90_all27c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all27d <- summary (mols90_all27d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_mols90_all37a <- summary (mols90_all37a <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party90, fm03_clname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all37b <- summary (mols90_all37b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party90, fm02_contall, fm03_cfeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all37c <- summary (mols90_all37c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party90, fm02_contall, fm03_ifeclname)), data=filter (lmonth3, month_id_period >= 4))))
(sum_mols90_all37d <- summary (mols90_all37d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party90, fm03_lmfeclname)), data=filter (lmonth3, month_id_period >= 4))))



#### (3.2.6) 90-day window, survival specifications ####

## (3.2.6.0) no interactions

# outcome is resignation
(sum_surv90_all10a <- summ (surv90_all10a <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_main90, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)
(sum_surv90_all10b <- summ (surv90_all10b <- glm (
  as.formula (paste0 ("resignation_m", " ~ ", fm01_main90, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + elected position
(sum_surv90_all20a <- summ (surv90_all20a <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_main90, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)
(sum_surv90_all20b <- summ (surv90_all20b <- glm (
  as.formula (paste0 ("officeNext_elected", " ~ ", fm01_main90, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)

# outcome is resignation + appointed position
(sum_surv90_all30a <- summ (surv90_all30a <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_main90, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)
(sum_surv90_all30b <- summ (surv90_all30b <- glm (
  as.formula (paste0 ("officeNext_appointed", " ~ ", fm01_main90, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)

# outcome is death
(sum_surv90_all40a <- summ (surv90_all40a <- glm (
  as.formula (paste0 ("death_m", " ~ ", fm01_main90, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)
(sum_surv90_all40b <- summ (surv90_all40b <- glm (
  as.formula (paste0 ("death_m", " ~ ", fm01_main90, fm02_contall_glm, fm03_durfe))
  , family=binomial(link="cloglog"), data=filter (lmonth3, month_id_period >= 4)), robust="HC1", cluster="name")$coeftable)


## removing the models, that take A LOT of space
rm (
  surv90_all10a, surv90_all10b, surv90_all20a, surv90_all20b, surv90_all30a, surv90_all30b)





#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 4 // ALTERNATIVE DIFF-IN-DIFF SPECIFICATIONS ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

###### (4.1) Imai, Kim and Wang (2020) matching + DiD estimator

#### (4.1.1) creating the dataset w/matched sets
lmonth4 <- lmonth3 %>%
  
  ## getting rid of columns we don't want
  select (-one_of (c (colnames (lmonth3)[grep ('60', colnames (lmonth3))], colnames (lmonth3)[grep ('90', colnames (lmonth3))]))) %>% 
  
  
  ## we must exclude months 0, 1, 2 or 3 b/c if the alternation took place in December of the year the legislator assumed office, the first two months will identify the change in treatment status that occurred BEFORE the legislator assumed office; and the between month 1 (treated) and month 2 (not treated), there is a 'change' in treatment status
  filter (month_id_period >= 3) %>% group_by (district, period, name) %>% mutate (
    
    
    ## getting leads and lags of the outcome variables
    
    # resignation
    resignation_m100_lag05 = lag (resignation_m100, n=5, order_by=month_id_period)
    , resignation_m100_lag04 = lag (resignation_m100, n=4, order_by=month_id_period)
    , resignation_m100_lag03 = lag (resignation_m100, n=3, order_by=month_id_period)
    , resignation_m100_lag02 = lag (resignation_m100, n=2, order_by=month_id_period)
    , resignation_m100_lag01 = lag (resignation_m100, n=1, order_by=month_id_period)
    
    , resignation_m100_lead01 = lead (resignation_m100, n=1, order_by=month_id_period)
    , resignation_m100_lead02 = lead (resignation_m100, n=2, order_by=month_id_period)
    , resignation_m100_lead03 = lead (resignation_m100, n=3, order_by=month_id_period)
    , resignation_m100_lead04 = lead (resignation_m100, n=4, order_by=month_id_period)
    , resignation_m100_lead05 = lead (resignation_m100, n=5, order_by=month_id_period)
    
    # resignation and election
    , officeNext_elected100_lag05 = lag (officeNext_elected100, n=5, order_by=month_id_period)
    , officeNext_elected100_lag04 = lag (officeNext_elected100, n=4, order_by=month_id_period)
    , officeNext_elected100_lag03 = lag (officeNext_elected100, n=3, order_by=month_id_period)
    , officeNext_elected100_lag02 = lag (officeNext_elected100, n=2, order_by=month_id_period)
    , officeNext_elected100_lag01 = lag (officeNext_elected100, n=1, order_by=month_id_period)
    
    , officeNext_elected100_lead01 = lead (officeNext_elected100, n=1, order_by=month_id_period)
    , officeNext_elected100_lead02 = lead (officeNext_elected100, n=2, order_by=month_id_period)
    , officeNext_elected100_lead03 = lead (officeNext_elected100, n=3, order_by=month_id_period)
    , officeNext_elected100_lead04 = lead (officeNext_elected100, n=4, order_by=month_id_period)
    , officeNext_elected100_lead05 = lead (officeNext_elected100, n=5, order_by=month_id_period)
    
    # resignation and appointed
    , officeNext_appointed100_lag05 = lag (officeNext_appointed100, n=5, order_by=month_id_period)
    , officeNext_appointed100_lag04 = lag (officeNext_appointed100, n=4, order_by=month_id_period)
    , officeNext_appointed100_lag03 = lag (officeNext_appointed100, n=3, order_by=month_id_period)
    , officeNext_appointed100_lag02 = lag (officeNext_appointed100, n=2, order_by=month_id_period)
    , officeNext_appointed100_lag01 = lag (officeNext_appointed100, n=1, order_by=month_id_period)
    
    , officeNext_appointed100_lead01 = lead (officeNext_appointed100, n=1, order_by=month_id_period)
    , officeNext_appointed100_lead02 = lead (officeNext_appointed100, n=2, order_by=month_id_period)
    , officeNext_appointed100_lead03 = lead (officeNext_appointed100, n=3, order_by=month_id_period)
    , officeNext_appointed100_lead04 = lead (officeNext_appointed100, n=4, order_by=month_id_period)
    , officeNext_appointed100_lead05 = lead (officeNext_appointed100, n=5, order_by=month_id_period)
    
    
    ## transforming the (continuous) 30-day alternation variables into dummies
    , alternation30_nat_spsp = ifelse (alternation30_nat_spsp > 0, 1, 0)
    , alternation30_nat_spdp = ifelse (alternation30_nat_spdp > 0, 1, 0)
    , alternation30_nat_dpdp = ifelse (alternation30_nat_dpdp > 0, 1, 0)
    , alternation30_prov_spsp = ifelse (alternation30_prov_spsp > 0, 1, 0)
    , alternation30_prov_spdp = ifelse (alternation30_prov_spdp > 0, 1, 0)
    , alternation30_prov_dpdp = ifelse (alternation30_prov_dpdp > 0, 1, 0)
    
    
    ## identifying individuals that belong to each treatment group
    , alternation30_nat_spsp_treat = max.na (alternation30_nat_spsp)
    , alternation30_nat_spdp_treat = max.na (alternation30_nat_spdp)
    , alternation30_nat_dpdp_treat = max.na (alternation30_nat_dpdp)
    , alternation30_prov_spsp_treat = max.na (alternation30_prov_spsp)
    , alternation30_prov_spdp_treat = max.na (alternation30_prov_spdp)
    , alternation30_prov_dpdp_treat = max.na (alternation30_prov_dpdp)
    
    
    ## getting lags of all treatment variables
    , alternation30_nat_spsp_lag05 = lag (alternation30_nat_spsp, n=5, order_by=month_id_period)
    , alternation30_nat_spdp_lag05 = lag (alternation30_nat_spdp, n=5, order_by=month_id_period)
    , alternation30_nat_dpdp_lag05 = lag (alternation30_nat_dpdp, n=5, order_by=month_id_period)
    , alternation30_prov_spsp_lag05 = lag (alternation30_prov_spsp, n=5, order_by=month_id_period)
    , alternation30_prov_spdp_lag05 = lag (alternation30_prov_spdp, n=5, order_by=month_id_period)
    , alternation30_prov_dpdp_lag05 = lag (alternation30_prov_dpdp, n=5, order_by=month_id_period)
    
    , alternation30_nat_spsp_lag04 = lag (alternation30_nat_spsp, n=4, order_by=month_id_period)
    , alternation30_nat_spdp_lag04 = lag (alternation30_nat_spdp, n=4, order_by=month_id_period)
    , alternation30_nat_dpdp_lag04 = lag (alternation30_nat_dpdp, n=4, order_by=month_id_period)
    , alternation30_prov_spsp_lag04 = lag (alternation30_prov_spsp, n=4, order_by=month_id_period)
    , alternation30_prov_spdp_lag04 = lag (alternation30_prov_spdp, n=4, order_by=month_id_period)
    , alternation30_prov_dpdp_lag04 = lag (alternation30_prov_dpdp, n=4, order_by=month_id_period)
    
    , alternation30_nat_spsp_lag03 = lag (alternation30_nat_spsp, n=3, order_by=month_id_period)
    , alternation30_nat_spdp_lag03 = lag (alternation30_nat_spdp, n=3, order_by=month_id_period)
    , alternation30_nat_dpdp_lag03 = lag (alternation30_nat_dpdp, n=3, order_by=month_id_period)
    , alternation30_prov_spsp_lag03 = lag (alternation30_prov_spsp, n=3, order_by=month_id_period)
    , alternation30_prov_spdp_lag03 = lag (alternation30_prov_spdp, n=3, order_by=month_id_period)
    , alternation30_prov_dpdp_lag03 = lag (alternation30_prov_dpdp, n=3, order_by=month_id_period)
    
    , alternation30_nat_spsp_lag02 = lag (alternation30_nat_spsp, n=2, order_by=month_id_period)
    , alternation30_nat_spdp_lag02 = lag (alternation30_nat_spdp, n=2, order_by=month_id_period)
    , alternation30_nat_dpdp_lag02 = lag (alternation30_nat_dpdp, n=2, order_by=month_id_period)
    , alternation30_prov_spsp_lag02 = lag (alternation30_prov_spsp, n=2, order_by=month_id_period)
    , alternation30_prov_spdp_lag02 = lag (alternation30_prov_spdp, n=2, order_by=month_id_period)
    , alternation30_prov_dpdp_lag02 = lag (alternation30_prov_dpdp, n=2, order_by=month_id_period)
    
    , alternation30_nat_spsp_lag01 = lag (alternation30_nat_spsp, n=1, order_by=month_id_period)
    , alternation30_nat_spdp_lag01 = lag (alternation30_nat_spdp, n=1, order_by=month_id_period)
    , alternation30_nat_dpdp_lag01 = lag (alternation30_nat_dpdp, n=1, order_by=month_id_period)
    , alternation30_prov_spsp_lag01 = lag (alternation30_prov_spsp, n=1, order_by=month_id_period)
    , alternation30_prov_spdp_lag01 = lag (alternation30_prov_spdp, n=1, order_by=month_id_period)
    , alternation30_prov_dpdp_lag01 = lag (alternation30_prov_dpdp, n=1, order_by=month_id_period)
    
    
    ## identifying the first month in which an individual received the treatment of interest, if applicable
    
    # we first identify all months in which the treatment status changed
    , alternation30_nat_spsp_ch = ifelse (alternation30_nat_spsp==alternation30_nat_spsp_lag01, 9999, month_id_period) 
    , alternation30_nat_spdp_ch = ifelse (alternation30_nat_spdp==alternation30_nat_spdp_lag01, 9999, month_id_period)
    , alternation30_nat_dpdp_ch = ifelse (alternation30_nat_dpdp==alternation30_nat_dpdp_lag01, 9999, month_id_period)
    , alternation30_prov_spsp_ch = ifelse (alternation30_prov_spsp==alternation30_prov_spsp_lag01, 9999, month_id_period)
    , alternation30_prov_spdp_ch = ifelse (alternation30_prov_spdp==alternation30_prov_spdp_lag01, 9999, month_id_period)
    , alternation30_prov_dpdp_ch = ifelse (alternation30_prov_dpdp==alternation30_prov_dpdp_lag01, 9999, month_id_period)
    
    # we then identify the first period in which treatment status changed
    , alternation30_nat_spsp_ch = min.na (alternation30_nat_spsp_ch)
    , alternation30_nat_spdp_ch = min.na (alternation30_nat_spdp_ch)
    , alternation30_nat_dpdp_ch = min.na (alternation30_nat_dpdp_ch)
    , alternation30_prov_spsp_ch = min.na (alternation30_prov_spsp_ch)
    , alternation30_prov_spdp_ch = min.na (alternation30_prov_spdp_ch)
    , alternation30_prov_dpdp_ch = min.na (alternation30_prov_dpdp_ch)
    
    
    ## creating the matched sets
    # matched set: group of observations that share identical values in several values SIMULTANEOUSLY: time-invariant controls; (lagged) month_id_period; and lagged treatment(s)
  ) %>% ungroup () %>% mutate (
    
    ## first match set, no district or legislative period identifier
    mset01 = str_c (
      
      # time-invariant controls
      str_replace_na (party), str_replace_na (midterm), str_replace_na (mag), str_replace_na (seats), str_replace_na (position), str_replace_na (vulnerable), str_replace_na (chair), str_replace_na (exe.b), str_replace_na (leg.b), str_replace_na (female)
      
      # month (within legislative term)
      , str_replace_na (month_id_period -5), str_replace_na (month_id_period -4), str_replace_na (month_id_period -3), str_replace_na (month_id_period -2), str_replace_na (month_id_period -1), str_replace_na (month_id_period)
      
      # lagged treatment(s)
      , str_c (str_replace_na (alternation30_nat_spsp_lag05), str_replace_na (alternation30_nat_spsp_lag04), str_replace_na (alternation30_nat_spsp_lag03), str_replace_na (alternation30_nat_spsp_lag02), str_replace_na (alternation30_nat_spsp_lag01), sep='')
      , str_c (str_replace_na (alternation30_nat_spdp_lag05), str_replace_na (alternation30_nat_spdp_lag04), str_replace_na (alternation30_nat_spdp_lag03), str_replace_na (alternation30_nat_spdp_lag02), str_replace_na (alternation30_nat_spdp_lag01), sep='')
      , str_c (str_replace_na (alternation30_nat_dpdp_lag05), str_replace_na (alternation30_nat_dpdp_lag04), str_replace_na (alternation30_nat_dpdp_lag03), str_replace_na (alternation30_nat_dpdp_lag02), str_replace_na (alternation30_nat_dpdp_lag01), sep='')
      , str_c (str_replace_na (alternation30_prov_spsp_lag05), str_replace_na (alternation30_prov_spsp_lag04), str_replace_na (alternation30_prov_spsp_lag03), str_replace_na (alternation30_prov_spsp_lag02), str_replace_na (alternation30_prov_spsp_lag01), sep='')
      , str_c (str_replace_na (alternation30_prov_spdp_lag05), str_replace_na (alternation30_prov_spdp_lag04), str_replace_na (alternation30_prov_spdp_lag03), str_replace_na (alternation30_prov_spdp_lag02), str_replace_na (alternation30_prov_spdp_lag01), sep='')
      , str_c (str_replace_na (alternation30_prov_dpdp_lag05), str_replace_na (alternation30_prov_dpdp_lag04), str_replace_na (alternation30_prov_dpdp_lag03), str_replace_na (alternation30_prov_dpdp_lag02), str_replace_na (alternation30_prov_dpdp_lag01), sep='')
      , sep='_')
    
    
    ## second & third match sets, adding district or period identifiers
    , mset02 = str_c (mset01, district, sep='_')
    , mset03 = str_c (mset01, period, sep='_')
    
    
    ## adding a month_id_period identifier to each matched set
    , mset01_month = str_c (mset01, month_id_period, sep='_')
    , mset02_month = str_c (mset02, month_id_period, sep='_')
    , mset03_month = str_c (mset03, month_id_period, sep='_') )
summary (lmonth4)


## looking at some descriptive data -> there is much more variation in alternation at the provincial than at the national level
with (lmonth4, table (alternation30_nat_spsp_treat, alternation30_nat_spsp_ch))
with (lmonth4, table (alternation30_prov_spsp_treat, alternation30_prov_spsp_ch))
with (lmonth4, table (alternation30_nat_spdp_treat, alternation30_nat_spdp_ch))
with (lmonth4, table (alternation30_prov_spdp_treat, alternation30_prov_spdp_ch))
with (lmonth4, table (alternation30_nat_dpdp_treat, alternation30_nat_dpdp_ch))
with (lmonth4, table (alternation30_prov_dpdp_treat, alternation30_prov_dpdp_ch))


## we then get the subset of observations that were treated, in the month in which they were treated exclusively:
mset_nat_spsp_treat <- filter (lmonth4, month_id_period == alternation30_nat_spsp_ch)
mset_nat_spdp_treat <- filter (lmonth4, month_id_period == alternation30_nat_spdp_ch)
mset_nat_dpdp_treat <- filter (lmonth4, month_id_period == alternation30_nat_dpdp_ch)
mset_prov_spsp_treat <- filter (lmonth4, month_id_period == alternation30_prov_spsp_ch)
mset_prov_spdp_treat <- filter (lmonth4, month_id_period == alternation30_prov_spdp_ch)
mset_prov_dpdp_treat <- filter (lmonth4, month_id_period == alternation30_prov_dpdp_ch)


## for each of these subsets, we identify all observations within the same matched set that did NOT receive treatment in the same month
mdata <- bind_rows (
  
  # national alternation, same party, same person
  bind_rows (mset_nat_spsp_treat, filter (lmonth4, alternation30_nat_spsp_treat==0
                                          & mset01_month %in% unique (mset_nat_spsp_treat$mset01_month))
  ) %>% mutate (mset='basic', tr_type='nat_spsp', tr_status=alternation30_nat_spsp_treat)
  , bind_rows (mset_nat_spsp_treat, filter (lmonth4, alternation30_nat_spsp_treat==0
                                            & mset02_month %in% unique (mset_nat_spsp_treat$mset02_month))
  ) %>% mutate (mset='district', tr_type='nat_spsp', tr_status=alternation30_nat_spsp_treat)
  , bind_rows (mset_nat_spsp_treat, filter (lmonth4, alternation30_nat_spsp_treat==0
                                            & mset03_month %in% unique (mset_nat_spsp_treat$mset03_month))
  ) %>% mutate (mset='period', tr_type='nat_spsp', tr_status=alternation30_nat_spsp_treat)
  
  # national alternation, same party, different person
  , bind_rows (mset_nat_spdp_treat, filter (lmonth4, alternation30_nat_spdp_treat==0
                                            & mset01_month %in% unique (mset_nat_spdp_treat$mset01_month))
  ) %>% mutate (mset='basic', tr_type='nat_spdp', tr_status=alternation30_nat_spdp_treat)
  , bind_rows (mset_nat_spdp_treat, filter (lmonth4, alternation30_nat_spdp_treat==0
                                            & mset02_month %in% unique (mset_nat_spdp_treat$mset02_month))
  ) %>% mutate (mset='district', tr_type='nat_spdp', tr_status=alternation30_nat_spdp_treat)
  , bind_rows (mset_nat_spdp_treat, filter (lmonth4, alternation30_nat_spdp_treat==0
                                            & mset03_month %in% unique (mset_nat_spdp_treat$mset03_month))
  ) %>% mutate (mset='period', tr_type='nat_spdp', tr_status=alternation30_nat_spdp_treat)
  
  # national alternation, different party, different person
  , bind_rows (mset_nat_dpdp_treat, filter (lmonth4, alternation30_nat_dpdp_treat==0
                                            & mset01_month %in% unique (mset_nat_dpdp_treat$mset01_month))
  ) %>% mutate (mset='basic', tr_type='nat_dpdp', tr_status=alternation30_nat_dpdp_treat)
  , bind_rows (mset_nat_dpdp_treat, filter (lmonth4, alternation30_nat_dpdp_treat==0
                                            & mset02_month %in% unique (mset_nat_dpdp_treat$mset02_month))
  ) %>% mutate (mset='district', tr_type='nat_dpdp', tr_status=alternation30_nat_dpdp_treat)
  , bind_rows (mset_nat_dpdp_treat, filter (lmonth4, alternation30_nat_dpdp_treat==0
                                            & mset03_month %in% unique (mset_nat_dpdp_treat$mset03_month))
  ) %>% mutate (mset='period', tr_type='nat_dpdp', tr_status=alternation30_nat_dpdp_treat)
  
  # provincial alternation, same party, same person
  , bind_rows (mset_prov_spsp_treat, filter (lmonth4, alternation30_prov_spsp_treat==0
                                             & mset01_month %in% unique (mset_prov_spsp_treat$mset01_month))
  ) %>% mutate (mset='basic', tr_type='prov_spsp', tr_status=alternation30_prov_spsp_treat)
  , bind_rows (mset_prov_spsp_treat, filter (lmonth4, alternation30_prov_spsp_treat==0
                                             & mset02_month %in% unique (mset_prov_spsp_treat$mset02_month))
  ) %>% mutate (mset='district', tr_type='prov_spsp', tr_status=alternation30_prov_spsp_treat)
  , bind_rows (mset_prov_spsp_treat, filter (lmonth4, alternation30_prov_spsp_treat==0
                                             & mset03_month %in% unique (mset_prov_spsp_treat$mset03_month))
  ) %>% mutate (mset='period', tr_type='prov_spsp', tr_status=alternation30_prov_spsp_treat)
  
  # provincial alternation, same party, different person
  , bind_rows (mset_prov_spdp_treat, filter (lmonth4, alternation30_prov_spdp_treat==0
                                             & mset01_month %in% unique (mset_prov_spdp_treat$mset01_month))
  ) %>% mutate (mset='basic', tr_type='prov_spdp', tr_status=alternation30_prov_spdp_treat)
  , bind_rows (mset_prov_spdp_treat, filter (lmonth4, alternation30_prov_spdp_treat==0
                                             & mset02_month %in% unique (mset_prov_spdp_treat$mset02_month))
  ) %>% mutate (mset='district', tr_type='prov_spdp', tr_status=alternation30_prov_spdp_treat)
  , bind_rows (mset_prov_spdp_treat, filter (lmonth4, alternation30_prov_spdp_treat==0
                                             & mset03_month %in% unique (mset_prov_spdp_treat$mset03_month))
  ) %>% mutate (mset='period', tr_type='prov_spdp', tr_status=alternation30_prov_spdp_treat)
  
  # provincial alternation, different party, different person
  , bind_rows (mset_prov_dpdp_treat, filter (lmonth4, alternation30_prov_dpdp_treat==0
                                             & mset01_month %in% unique (mset_prov_dpdp_treat$mset01_month))
  ) %>% mutate (mset='basic', tr_type='prov_dpdp', tr_status=alternation30_prov_dpdp_treat)
  , bind_rows (mset_prov_dpdp_treat, filter (lmonth4, alternation30_prov_dpdp_treat==0
                                             & mset02_month %in% unique (mset_prov_dpdp_treat$mset02_month))
  ) %>% mutate (mset='district', tr_type='prov_dpdp', tr_status=alternation30_prov_dpdp_treat)
  , bind_rows (mset_prov_dpdp_treat, filter (lmonth4, alternation30_prov_dpdp_treat==0
                                             & mset03_month %in% unique (mset_prov_dpdp_treat$mset03_month))
  ) %>% mutate (mset='period', tr_type='prov_dpdp', tr_status=alternation30_prov_dpdp_treat)
  
) %>% mutate (
  mset = factor (mset, levels=c ('basic', 'district', 'period'))
  , tr_type = factor (tr_type, levels=c ('nat_spsp', 'nat_spdp', 'nat_dpdp', 'prov_spsp', 'prov_spdp', 'prov_dpdp'))
  
  
  ## getting information about each matched set
  ) %>% group_by (mset01_month, mset, tr_type) %>% mutate (
  nset = n()
  , ntreat = sum (tr_status)
  , ncont = sum (1 - tr_status)
  , weight = 1/nset
  , exclude = ifelse (nset == ntreat | nset == ncont, 1, 0) )
summary (select (mdata, id:days, mset01:exclude
                 , resignation_m100, resignation_m100_lag05:resignation_m100_lead05
                 , officeNext_elected100, officeNext_elected100_lag05:officeNext_elected100_lead05
                 , officeNext_appointed100, officeNext_appointed100_lag05:officeNext_appointed100_lead05))



### (4.1.2) estimating the models

## (4.1.2.1) 'basic' matched set (i.e., not adding neither district nor period)

## national alternation, same party, same person

# outcome is resignation
(sum_ikw_nat_spsp110 <- summary (ikw_nat_spsp110 <- felm (resignation_m100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp111 <- summary (ikw_nat_spsp111 <- felm (resignation_m100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp112 <- summary (ikw_nat_spsp112 <- felm (resignation_m100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp113 <- summary (ikw_nat_spsp113 <- felm (resignation_m100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp114 <- summary (ikw_nat_spsp114 <- felm (resignation_m100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp115 <- summary (ikw_nat_spsp115 <- felm (resignation_m100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))

# outcome is resignation + election
(sum_ikw_nat_spsp120 <- summary (ikw_nat_spsp120 <- felm (officeNext_elected100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp121 <- summary (ikw_nat_spsp121 <- felm (officeNext_elected100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp122 <- summary (ikw_nat_spsp122 <- felm (officeNext_elected100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp123 <- summary (ikw_nat_spsp123 <- felm (officeNext_elected100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp124 <- summary (ikw_nat_spsp124 <- felm (officeNext_elected100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp125 <- summary (ikw_nat_spsp125 <- felm (officeNext_elected100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))

# outcome is resignation + appointment
(sum_ikw_nat_spsp130 <- summary (ikw_nat_spsp130 <- felm (officeNext_appointed100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp131 <- summary (ikw_nat_spsp131 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp132 <- summary (ikw_nat_spsp132 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp133 <- summary (ikw_nat_spsp133 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp134 <- summary (ikw_nat_spsp134 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp135 <- summary (ikw_nat_spsp135 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spsp')$weight)))


## national alternation, same party, different person

# outcome is resignation
(sum_ikw_nat_spdp110 <- summary (ikw_nat_spdp110 <- felm (resignation_m100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp111 <- summary (ikw_nat_spdp111 <- felm (resignation_m100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp112 <- summary (ikw_nat_spdp112 <- felm (resignation_m100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp113 <- summary (ikw_nat_spdp113 <- felm (resignation_m100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp114 <- summary (ikw_nat_spdp114 <- felm (resignation_m100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp115 <- summary (ikw_nat_spdp115 <- felm (resignation_m100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))

# outcome is resignation + election
(sum_ikw_nat_spdp120 <- summary (ikw_nat_spdp120 <- felm (officeNext_elected100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp121 <- summary (ikw_nat_spdp121 <- felm (officeNext_elected100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp122 <- summary (ikw_nat_spdp122 <- felm (officeNext_elected100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp123 <- summary (ikw_nat_spdp123 <- felm (officeNext_elected100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp124 <- summary (ikw_nat_spdp124 <- felm (officeNext_elected100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp125 <- summary (ikw_nat_spdp125 <- felm (officeNext_elected100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_nat_spdp130 <- summary (ikw_nat_spdp130 <- felm (officeNext_appointed100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp131 <- summary (ikw_nat_spdp131 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp132 <- summary (ikw_nat_spdp132 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp133 <- summary (ikw_nat_spdp133 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp134 <- summary (ikw_nat_spdp134 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp135 <- summary (ikw_nat_spdp135 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_spdp')$weight)))


## national alternation, different party, different person

# outcome is resignation
(sum_ikw_nat_dpdp110 <- summary (ikw_nat_dpdp110 <- felm (resignation_m100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp111 <- summary (ikw_nat_dpdp111 <- felm (resignation_m100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp112 <- summary (ikw_nat_dpdp112 <- felm (resignation_m100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp113 <- summary (ikw_nat_dpdp113 <- felm (resignation_m100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp114 <- summary (ikw_nat_dpdp114 <- felm (resignation_m100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp115 <- summary (ikw_nat_dpdp115 <- felm (resignation_m100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))

# outcome is resignation + election
(sum_ikw_nat_dpdp120 <- summary (ikw_nat_dpdp120 <- felm (officeNext_elected100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp121 <- summary (ikw_nat_dpdp121 <- felm (officeNext_elected100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp122 <- summary (ikw_nat_dpdp122 <- felm (officeNext_elected100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp123 <- summary (ikw_nat_dpdp123 <- felm (officeNext_elected100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp124 <- summary (ikw_nat_dpdp124 <- felm (officeNext_elected100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp125 <- summary (ikw_nat_dpdp125 <- felm (officeNext_elected100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_nat_dpdp130 <- summary (ikw_nat_dpdp130 <- felm (officeNext_appointed100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp131 <- summary (ikw_nat_dpdp131 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp132 <- summary (ikw_nat_dpdp132 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp133 <- summary (ikw_nat_dpdp133 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp134 <- summary (ikw_nat_dpdp134 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp135 <- summary (ikw_nat_dpdp135 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='nat_dpdp')$weight)))


## provincial alternation, same party, same person

# outcome is resignation
(sum_ikw_prov_spsp110 <- summary (ikw_prov_spsp110 <- felm (resignation_m100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp111 <- summary (ikw_prov_spsp111 <- felm (resignation_m100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp112 <- summary (ikw_prov_spsp112 <- felm (resignation_m100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp113 <- summary (ikw_prov_spsp113 <- felm (resignation_m100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp114 <- summary (ikw_prov_spsp114 <- felm (resignation_m100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp115 <- summary (ikw_prov_spsp115 <- felm (resignation_m100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))

# outcome is resignation + election
(sum_ikw_prov_spsp120 <- summary (ikw_prov_spsp120 <- felm (officeNext_elected100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp121 <- summary (ikw_prov_spsp121 <- felm (officeNext_elected100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp122 <- summary (ikw_prov_spsp122 <- felm (officeNext_elected100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp123 <- summary (ikw_prov_spsp123 <- felm (officeNext_elected100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp124 <- summary (ikw_prov_spsp124 <- felm (officeNext_elected100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp125 <- summary (ikw_prov_spsp125 <- felm (officeNext_elected100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))

# outcome is resignation + appointment
(sum_ikw_prov_spsp130 <- summary (ikw_prov_spsp130 <- felm (officeNext_appointed100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp131 <- summary (ikw_prov_spsp131 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp132 <- summary (ikw_prov_spsp132 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp133 <- summary (ikw_prov_spsp133 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp134 <- summary (ikw_prov_spsp134 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp135 <- summary (ikw_prov_spsp135 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spsp')$weight)))


## provincial alternation, same party, different person

# outcome is resignation
(sum_ikw_prov_spdp110 <- summary (ikw_prov_spdp110 <- felm (resignation_m100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp111 <- summary (ikw_prov_spdp111 <- felm (resignation_m100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp112 <- summary (ikw_prov_spdp112 <- felm (resignation_m100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp113 <- summary (ikw_prov_spdp113 <- felm (resignation_m100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp114 <- summary (ikw_prov_spdp114 <- felm (resignation_m100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp115 <- summary (ikw_prov_spdp115 <- felm (resignation_m100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))

# outcome is resignation + election
(sum_ikw_prov_spdp120 <- summary (ikw_prov_spdp120 <- felm (officeNext_elected100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp121 <- summary (ikw_prov_spdp121 <- felm (officeNext_elected100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp122 <- summary (ikw_prov_spdp122 <- felm (officeNext_elected100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp123 <- summary (ikw_prov_spdp123 <- felm (officeNext_elected100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp124 <- summary (ikw_prov_spdp124 <- felm (officeNext_elected100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp125 <- summary (ikw_prov_spdp125 <- felm (officeNext_elected100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_prov_spdp130 <- summary (ikw_prov_spdp130 <- felm (officeNext_appointed100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp131 <- summary (ikw_prov_spdp131 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp132 <- summary (ikw_prov_spdp132 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp133 <- summary (ikw_prov_spdp133 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp134 <- summary (ikw_prov_spdp134 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp135 <- summary (ikw_prov_spdp135 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_spdp')$weight)))


## provincial alternation, different party, different person

# outcome is resignation
(sum_ikw_prov_dpdp110 <- summary (ikw_prov_dpdp110 <- felm (resignation_m100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp111 <- summary (ikw_prov_dpdp111 <- felm (resignation_m100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp112 <- summary (ikw_prov_dpdp112 <- felm (resignation_m100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp113 <- summary (ikw_prov_dpdp113 <- felm (resignation_m100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp114 <- summary (ikw_prov_dpdp114 <- felm (resignation_m100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp115 <- summary (ikw_prov_dpdp115 <- felm (resignation_m100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))

# outcome is resignation + election
(sum_ikw_prov_dpdp120 <- summary (ikw_prov_dpdp120 <- felm (officeNext_elected100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp121 <- summary (ikw_prov_dpdp121 <- felm (officeNext_elected100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp122 <- summary (ikw_prov_dpdp122 <- felm (officeNext_elected100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp123 <- summary (ikw_prov_dpdp123 <- felm (officeNext_elected100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp124 <- summary (ikw_prov_dpdp124 <- felm (officeNext_elected100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp125 <- summary (ikw_prov_dpdp125 <- felm (officeNext_elected100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_prov_dpdp130 <- summary (ikw_prov_dpdp130 <- felm (officeNext_appointed100 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp131 <- summary (ikw_prov_dpdp131 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp132 <- summary (ikw_prov_dpdp132 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp133 <- summary (ikw_prov_dpdp133 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp134 <- summary (ikw_prov_dpdp134 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp135 <- summary (ikw_prov_dpdp135 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset01_month | 0 | mset01_month, data=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='basic' & tr_type=='prov_dpdp')$weight)))


## (4.1.2.2) 'district' matched set (i.e., comparing deputies from the same province)

## national alternation, same party, same person

# outcome is resignation
(sum_ikw_nat_spsp210 <- summary (ikw_nat_spsp210 <- felm (resignation_m100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp211 <- summary (ikw_nat_spsp211 <- felm (resignation_m100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp212 <- summary (ikw_nat_spsp212 <- felm (resignation_m100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp213 <- summary (ikw_nat_spsp213 <- felm (resignation_m100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp214 <- summary (ikw_nat_spsp214 <- felm (resignation_m100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp215 <- summary (ikw_nat_spsp215 <- felm (resignation_m100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))

# outcome is resignation + election
(sum_ikw_nat_spsp220 <- summary (ikw_nat_spsp220 <- felm (officeNext_elected100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp221 <- summary (ikw_nat_spsp221 <- felm (officeNext_elected100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp222 <- summary (ikw_nat_spsp222 <- felm (officeNext_elected100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp223 <- summary (ikw_nat_spsp223 <- felm (officeNext_elected100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp224 <- summary (ikw_nat_spsp224 <- felm (officeNext_elected100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp225 <- summary (ikw_nat_spsp225 <- felm (officeNext_elected100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))

# outcome is resignation + appointment
(sum_ikw_nat_spsp230 <- summary (ikw_nat_spsp230 <- felm (officeNext_appointed100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp231 <- summary (ikw_nat_spsp231 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp232 <- summary (ikw_nat_spsp232 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp233 <- summary (ikw_nat_spsp233 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp234 <- summary (ikw_nat_spsp234 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))
(sum_ikw_nat_spsp235 <- summary (ikw_nat_spsp235 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spsp')$weight)))


## national alternation, same party, different person

# outcome is resignation
(sum_ikw_nat_spdp210 <- summary (ikw_nat_spdp210 <- felm (resignation_m100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp211 <- summary (ikw_nat_spdp211 <- felm (resignation_m100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp212 <- summary (ikw_nat_spdp212 <- felm (resignation_m100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp213 <- summary (ikw_nat_spdp213 <- felm (resignation_m100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp214 <- summary (ikw_nat_spdp214 <- felm (resignation_m100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp215 <- summary (ikw_nat_spdp215 <- felm (resignation_m100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))

# outcome is resignation + election
(sum_ikw_nat_spdp220 <- summary (ikw_nat_spdp220 <- felm (officeNext_elected100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp221 <- summary (ikw_nat_spdp221 <- felm (officeNext_elected100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp222 <- summary (ikw_nat_spdp222 <- felm (officeNext_elected100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp223 <- summary (ikw_nat_spdp223 <- felm (officeNext_elected100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp224 <- summary (ikw_nat_spdp224 <- felm (officeNext_elected100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp225 <- summary (ikw_nat_spdp225 <- felm (officeNext_elected100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_nat_spdp230 <- summary (ikw_nat_spdp230 <- felm (officeNext_appointed100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp231 <- summary (ikw_nat_spdp231 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp232 <- summary (ikw_nat_spdp232 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp233 <- summary (ikw_nat_spdp233 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp234 <- summary (ikw_nat_spdp234 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))
(sum_ikw_nat_spdp235 <- summary (ikw_nat_spdp235 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_spdp')$weight)))


## national alternation, different party, different person

# outcome is resignation
(sum_ikw_nat_dpdp210 <- summary (ikw_nat_dpdp210 <- felm (resignation_m100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp211 <- summary (ikw_nat_dpdp211 <- felm (resignation_m100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp212 <- summary (ikw_nat_dpdp212 <- felm (resignation_m100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp213 <- summary (ikw_nat_dpdp213 <- felm (resignation_m100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp214 <- summary (ikw_nat_dpdp214 <- felm (resignation_m100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp215 <- summary (ikw_nat_dpdp215 <- felm (resignation_m100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))

# outcome is resignation + election
(sum_ikw_nat_dpdp220 <- summary (ikw_nat_dpdp220 <- felm (officeNext_elected100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp221 <- summary (ikw_nat_dpdp221 <- felm (officeNext_elected100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp222 <- summary (ikw_nat_dpdp222 <- felm (officeNext_elected100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp223 <- summary (ikw_nat_dpdp223 <- felm (officeNext_elected100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp224 <- summary (ikw_nat_dpdp224 <- felm (officeNext_elected100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp225 <- summary (ikw_nat_dpdp225 <- felm (officeNext_elected100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_nat_dpdp230 <- summary (ikw_nat_dpdp230 <- felm (officeNext_appointed100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp231 <- summary (ikw_nat_dpdp231 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp232 <- summary (ikw_nat_dpdp232 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp233 <- summary (ikw_nat_dpdp233 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp234 <- summary (ikw_nat_dpdp234 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))
(sum_ikw_nat_dpdp235 <- summary (ikw_nat_dpdp235 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='nat_dpdp')$weight)))


## provincial alternation, same party, same person

# outcome is resignation
(sum_ikw_prov_spsp210 <- summary (ikw_prov_spsp210 <- felm (resignation_m100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp211 <- summary (ikw_prov_spsp211 <- felm (resignation_m100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp212 <- summary (ikw_prov_spsp212 <- felm (resignation_m100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp213 <- summary (ikw_prov_spsp213 <- felm (resignation_m100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp214 <- summary (ikw_prov_spsp214 <- felm (resignation_m100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp215 <- summary (ikw_prov_spsp215 <- felm (resignation_m100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))

# outcome is resignation + election
(sum_ikw_prov_spsp220 <- summary (ikw_prov_spsp220 <- felm (officeNext_elected100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp221 <- summary (ikw_prov_spsp221 <- felm (officeNext_elected100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp222 <- summary (ikw_prov_spsp222 <- felm (officeNext_elected100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp223 <- summary (ikw_prov_spsp223 <- felm (officeNext_elected100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp224 <- summary (ikw_prov_spsp224 <- felm (officeNext_elected100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp225 <- summary (ikw_prov_spsp225 <- felm (officeNext_elected100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))

# outcome is resignation + appointment
(sum_ikw_prov_spsp230 <- summary (ikw_prov_spsp230 <- felm (officeNext_appointed100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp231 <- summary (ikw_prov_spsp231 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp232 <- summary (ikw_prov_spsp232 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp233 <- summary (ikw_prov_spsp233 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp234 <- summary (ikw_prov_spsp234 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))
(sum_ikw_prov_spsp235 <- summary (ikw_prov_spsp235 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spsp')$weight)))


## provincial alternation, same party, different person

# outcome is resignation
(sum_ikw_prov_spdp210 <- summary (ikw_prov_spdp210 <- felm (resignation_m100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp211 <- summary (ikw_prov_spdp211 <- felm (resignation_m100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp212 <- summary (ikw_prov_spdp212 <- felm (resignation_m100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp213 <- summary (ikw_prov_spdp213 <- felm (resignation_m100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp214 <- summary (ikw_prov_spdp214 <- felm (resignation_m100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp215 <- summary (ikw_prov_spdp215 <- felm (resignation_m100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))

# outcome is resignation + election
(sum_ikw_prov_spdp220 <- summary (ikw_prov_spdp220 <- felm (officeNext_elected100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp221 <- summary (ikw_prov_spdp221 <- felm (officeNext_elected100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp222 <- summary (ikw_prov_spdp222 <- felm (officeNext_elected100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp223 <- summary (ikw_prov_spdp223 <- felm (officeNext_elected100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp224 <- summary (ikw_prov_spdp224 <- felm (officeNext_elected100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp225 <- summary (ikw_prov_spdp225 <- felm (officeNext_elected100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_prov_spdp230 <- summary (ikw_prov_spdp230 <- felm (officeNext_appointed100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp231 <- summary (ikw_prov_spdp231 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp232 <- summary (ikw_prov_spdp232 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp233 <- summary (ikw_prov_spdp233 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp234 <- summary (ikw_prov_spdp234 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))
(sum_ikw_prov_spdp235 <- summary (ikw_prov_spdp235 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_spdp')$weight)))


## provincial alternation, different party, different person

# outcome is resignation
(sum_ikw_prov_dpdp210 <- summary (ikw_prov_dpdp210 <- felm (resignation_m100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp211 <- summary (ikw_prov_dpdp211 <- felm (resignation_m100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp212 <- summary (ikw_prov_dpdp212 <- felm (resignation_m100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp213 <- summary (ikw_prov_dpdp213 <- felm (resignation_m100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp214 <- summary (ikw_prov_dpdp214 <- felm (resignation_m100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp215 <- summary (ikw_prov_dpdp215 <- felm (resignation_m100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))

# outcome is resignation + election
(sum_ikw_prov_dpdp220 <- summary (ikw_prov_dpdp220 <- felm (officeNext_elected100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp221 <- summary (ikw_prov_dpdp221 <- felm (officeNext_elected100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp222 <- summary (ikw_prov_dpdp222 <- felm (officeNext_elected100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp223 <- summary (ikw_prov_dpdp223 <- felm (officeNext_elected100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp224 <- summary (ikw_prov_dpdp224 <- felm (officeNext_elected100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp225 <- summary (ikw_prov_dpdp225 <- felm (officeNext_elected100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))

# outcome is resignation + appointment
(sum_ikw_prov_dpdp230 <- summary (ikw_prov_dpdp230 <- felm (officeNext_appointed100 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp231 <- summary (ikw_prov_dpdp231 <- felm (officeNext_appointed100_lead01 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp232 <- summary (ikw_prov_dpdp232 <- felm (officeNext_appointed100_lead02 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp233 <- summary (ikw_prov_dpdp233 <- felm (officeNext_appointed100_lead03 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp234 <- summary (ikw_prov_dpdp234 <- felm (officeNext_appointed100_lead04 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))
(sum_ikw_prov_dpdp235 <- summary (ikw_prov_dpdp235 <- felm (officeNext_appointed100_lead05 ~ tr_status | mset02_month | 0 | mset02_month, data=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp'), weights=filter (mdata, exclude==0 & mset=='district' & tr_type=='prov_dpdp')$weight)))




###### (4.2) Callaway and Sant'Anna (2021) difference-in-differences estimator


###### WARNING!!!!
###### Estimating the Callaway and Sant'Anna models takes a LOT of time -- the actual value depends on your computer, but in a MacBook Air (Retina, 13-inch, 2018) with a 1.6 GHz Dual-Core Intel Core i5 processor and 8GB RAM it took a couple even days (admittedly while doing other stuff in the background). This is due to the fact that Callaway and Sant'Anna's estimator works slowly for unbalanced panels. Unfortunately, since legislators who resign are automatically removed from the sample, setting allow_unbalanced_panel=FALSE will not address the issue.
###### 



#### (4.2.1) Creating the necessary variables

## by district-name-period, get the first month a legislator received treatment
lmonth_csa <- lmonth3 %>% 
  filter (month_id_period >= 3) %>% 
  ## we must exclude months 0, 1, 2 or 3 b/c if the alternation took place in December of the year the legislator assumed office, the first two months will identify the change in treatment status that occurred BEFORE the legislator assumed office; and the between month 1 (treated) and month 2 (not treated), there is a 'change' in treatment status
  
  mutate (
    
    ## transforming the (continuous) 30-day alternation variables into dummies
    alternation30_nat_spsp_dum = ifelse (alternation30_nat_spsp > 0, 1, 0)
    , alternation30_nat_spdp_dum = ifelse (alternation30_nat_spdp > 0, 1, 0)
    , alternation30_nat_dpdp_dum = ifelse (alternation30_nat_dpdp > 0, 1, 0)
    , alternation30_prov_spsp_dum = ifelse (alternation30_prov_spsp > 0, 1, 0)
    , alternation30_prov_spdp_dum = ifelse (alternation30_prov_spdp > 0, 1, 0)
    , alternation30_prov_dpdp_dum = ifelse (alternation30_prov_dpdp > 0, 1, 0)
    
    ## getting the month*dummy variable
    , alternation30_nat_spsp_month = ifelse (alternation30_nat_spsp == 0, 9999, month_id_period)
    , alternation30_nat_spdp_month = ifelse (alternation30_nat_spdp == 0, 9999, month_id_period)
    , alternation30_nat_dpdp_month = ifelse (alternation30_nat_dpdp == 0, 9999, month_id_period)
    , alternation30_prov_spsp_month = ifelse (alternation30_prov_spsp == 0, 9999, month_id_period)
    , alternation30_prov_spdp_month = ifelse (alternation30_prov_spdp == 0, 9999, month_id_period)
    , alternation30_prov_dpdp_month = ifelse (alternation30_prov_dpdp == 0, 9999, month_id_period)
    
    
    ## identifying the first month in which an observation was treated
    ) %>% group_by (name, district, period) %>% mutate (
      alternation30_nat_spsp_month = min (alternation30_nat_spsp_month)
      , alternation30_nat_spdp_month = min (alternation30_nat_spdp_month)
      , alternation30_nat_dpdp_month = min (alternation30_nat_dpdp_month)
      , alternation30_prov_spsp_month = min (alternation30_prov_spsp_month)
      , alternation30_prov_spdp_month = min (alternation30_prov_spdp_month)
      , alternation30_prov_dpdp_month = min (alternation30_prov_dpdp_month)
    ) %>% ungroup () %>% mutate (
      alternation30_nat_spsp_month = ifelse (alternation30_nat_spsp_month == 9999, 0, alternation30_nat_spsp_month)
      , alternation30_nat_spdp_month = ifelse (alternation30_nat_spdp_month == 9999, 0, alternation30_nat_spdp_month)
      , alternation30_nat_dpdp_month = ifelse (alternation30_nat_dpdp_month == 9999, 0, alternation30_nat_dpdp_month)
      , alternation30_prov_spsp_month = ifelse (alternation30_prov_spsp_month == 9999, 0, alternation30_prov_spsp_month)
      , alternation30_prov_spdp_month = ifelse (alternation30_prov_spdp_month == 9999, 0, alternation30_prov_spdp_month)
      , alternation30_prov_dpdp_month = ifelse (alternation30_prov_dpdp_month == 9999, 0, alternation30_prov_dpdp_month) )
table (lmonth_csa$alternation30_prov_dpdp_month)
summary (lmonth_csa)



### (4.2.2) national alternation (within 30 days)

set.seed (18520203) ## for reproducibility

## (4.2.2.1) same party, same person

# no controls
csa_nat_spsp11 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp12 <- att_gt (yname='officeNext_elected100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp13 <- att_gt (yname='officeNext_appointed100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# adding controls --> b/c of perfect multicollinearity, we cannot control for midterm status. In addition, in some provincial models we cannot include controls, period. So we restrict controls to overall effects models
csa_nat_spsp21 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~female + incGober + voteSh + exe.b + leg.b + partyPJ + partyUCR + party3rd + log(seats) + positionRel + vulnerable, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')


## splitting the sample(s) (main outcome only)

# top position in list
csa_nat_spsp31 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa, top01==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp32 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa, top01==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# vulnerability
csa_nat_spsp41 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp42 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# previous experience
csa_nat_spsp51 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp52 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# committee chair
csa_nat_spsp61 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp62 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# elected in midterm election
csa_nat_spsp71 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp72 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# party ID (PJ and UCR only)
csa_nat_spsp81 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='PJ'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp82 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='UCR'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# gender
csa_nat_spsp91 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spsp92 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# event-study version(s)
csa_nat_spsp11_es <- aggte (csa_nat_spsp11, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp12_es <- aggte (csa_nat_spsp12, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp13_es <- aggte (csa_nat_spsp13, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp21_es <- aggte (csa_nat_spsp21, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp31_es <- aggte (csa_nat_spsp31, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp32_es <- aggte (csa_nat_spsp32, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp41_es <- aggte (csa_nat_spsp41, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp42_es <- aggte (csa_nat_spsp42, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp51_es <- aggte (csa_nat_spsp51, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp52_es <- aggte (csa_nat_spsp52, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp61_es <- aggte (csa_nat_spsp61, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp62_es <- aggte (csa_nat_spsp62, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp71_es <- aggte (csa_nat_spsp71, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp72_es <- aggte (csa_nat_spsp72, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp81_es <- aggte (csa_nat_spsp81, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp82_es <- aggte (csa_nat_spsp82, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp91_es <- aggte (csa_nat_spsp91, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spsp92_es <- aggte (csa_nat_spsp92, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)

# ATTs across groups
csa_nat_spsp11_gr <- aggte (csa_nat_spsp11, type='group', na.rm=TRUE)
csa_nat_spsp12_gr <- aggte (csa_nat_spsp12, type='group', na.rm=TRUE)
csa_nat_spsp13_gr <- aggte (csa_nat_spsp13, type='group', na.rm=TRUE)
csa_nat_spsp21_gr <- aggte (csa_nat_spsp21, type='group', na.rm=TRUE)
csa_nat_spsp31_gr <- aggte (csa_nat_spsp31, type='group', na.rm=TRUE)
csa_nat_spsp32_gr <- aggte (csa_nat_spsp32, type='group', na.rm=TRUE)
csa_nat_spsp41_gr <- aggte (csa_nat_spsp41, type='group', na.rm=TRUE)
csa_nat_spsp42_gr <- aggte (csa_nat_spsp42, type='group', na.rm=TRUE)
csa_nat_spsp51_gr <- aggte (csa_nat_spsp51, type='group', na.rm=TRUE)
csa_nat_spsp52_gr <- aggte (csa_nat_spsp52, type='group', na.rm=TRUE)
csa_nat_spsp61_gr <- aggte (csa_nat_spsp61, type='group', na.rm=TRUE)
csa_nat_spsp62_gr <- aggte (csa_nat_spsp62, type='group', na.rm=TRUE)
csa_nat_spsp71_gr <- aggte (csa_nat_spsp71, type='group', na.rm=TRUE)
csa_nat_spsp72_gr <- aggte (csa_nat_spsp72, type='group', na.rm=TRUE)
csa_nat_spsp81_gr <- aggte (csa_nat_spsp81, type='group', na.rm=TRUE)
csa_nat_spsp82_gr <- aggte (csa_nat_spsp82, type='group', na.rm=TRUE)
csa_nat_spsp91_gr <- aggte (csa_nat_spsp91, type='group', na.rm=TRUE)
csa_nat_spsp92_gr <- aggte (csa_nat_spsp92, type='group', na.rm=TRUE)


## (4.2.2.2) same party, different person

# no controls
csa_nat_spdp11 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp12 <- att_gt (yname='officeNext_elected100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp13 <- att_gt (yname='officeNext_appointed100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# adding controls
csa_nat_spdp21 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~female + incGober + voteSh + exe.b + leg.b + partyPJ + partyUCR + party3rd + log(seats) + positionRel + vulnerable, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
# csa_nat_spdp22 <- att_gt (yname='officeNext_elected100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~female + incGober + voteSh + exe.b + leg.b + partyPJ + partyUCR + party3rd + log(seats) + positionRel + vulnerable, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
# csa_nat_spdp23 <- att_gt (yname='officeNext_appointed100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~female + incGober + voteSh + exe.b + leg.b + partyPJ + partyUCR + party3rd + log(seats) + positionRel + vulnerable, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')


## splitting the sample(s) (main outcome only)

# top position in list
csa_nat_spdp31 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp32 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# vulnerability
csa_nat_spdp41 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp42 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# previous experience
csa_nat_spdp51 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp52 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# committee chair
csa_nat_spdp61 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp62 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# elected in midterm election
csa_nat_spdp71 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp72 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# party ID (PJ and UCR only)
csa_nat_spdp81 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='PJ'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp82 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='UCR'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# gender
csa_nat_spdp91 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_spdp92 <- att_gt (yname='resignation_m100', gname='alternation30_nat_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# event-study version(s)
csa_nat_spdp11_es <- aggte (csa_nat_spdp11, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp12_es <- aggte (csa_nat_spdp12, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp13_es <- aggte (csa_nat_spdp13, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp21_es <- aggte (csa_nat_spdp21, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp31_es <- aggte (csa_nat_spdp31, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp32_es <- aggte (csa_nat_spdp32, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp41_es <- aggte (csa_nat_spdp41, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp42_es <- aggte (csa_nat_spdp42, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp51_es <- aggte (csa_nat_spdp51, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp52_es <- aggte (csa_nat_spdp52, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp61_es <- aggte (csa_nat_spdp61, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp62_es <- aggte (csa_nat_spdp62, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp71_es <- aggte (csa_nat_spdp71, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp72_es <- aggte (csa_nat_spdp72, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp81_es <- aggte (csa_nat_spdp81, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp82_es <- aggte (csa_nat_spdp82, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp91_es <- aggte (csa_nat_spdp91, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_spdp92_es <- aggte (csa_nat_spdp92, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)

# ATTs across groups
csa_nat_spdp11_gr <- aggte (csa_nat_spdp11, type='group', na.rm=TRUE)
csa_nat_spdp12_gr <- aggte (csa_nat_spdp12, type='group', na.rm=TRUE)
csa_nat_spdp13_gr <- aggte (csa_nat_spdp13, type='group', na.rm=TRUE)
csa_nat_spdp21_gr <- aggte (csa_nat_spdp21, type='group', na.rm=TRUE)
csa_nat_spdp31_gr <- aggte (csa_nat_spdp31, type='group', na.rm=TRUE)
csa_nat_spdp32_gr <- aggte (csa_nat_spdp32, type='group', na.rm=TRUE)
csa_nat_spdp41_gr <- aggte (csa_nat_spdp41, type='group', na.rm=TRUE)
csa_nat_spdp42_gr <- aggte (csa_nat_spdp42, type='group', na.rm=TRUE)
csa_nat_spdp51_gr <- aggte (csa_nat_spdp51, type='group', na.rm=TRUE)
csa_nat_spdp52_gr <- aggte (csa_nat_spdp52, type='group', na.rm=TRUE)
csa_nat_spdp61_gr <- aggte (csa_nat_spdp61, type='group', na.rm=TRUE)
csa_nat_spdp62_gr <- aggte (csa_nat_spdp62, type='group', na.rm=TRUE)
csa_nat_spdp71_gr <- aggte (csa_nat_spdp71, type='group', na.rm=TRUE)
csa_nat_spdp72_gr <- aggte (csa_nat_spdp72, type='group', na.rm=TRUE)
csa_nat_spdp81_gr <- aggte (csa_nat_spdp81, type='group', na.rm=TRUE)
csa_nat_spdp82_gr <- aggte (csa_nat_spdp82, type='group', na.rm=TRUE)
csa_nat_spdp91_gr <- aggte (csa_nat_spdp91, type='group', na.rm=TRUE)
csa_nat_spdp92_gr <- aggte (csa_nat_spdp92, type='group', na.rm=TRUE)


## (4.2.2.3) different party, different person

# no controls
csa_nat_dpdp11 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp12 <- att_gt (yname='officeNext_elected100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp13 <- att_gt (yname='officeNext_appointed100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# adding controls
csa_nat_dpdp21 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~female + incGober + voteSh + exe.b + leg.b + partyPJ + partyUCR + party3rd + log(seats) + positionRel + vulnerable, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')


## splitting the sample(s) (main outcome only)

# top position in list
csa_nat_dpdp31 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp32 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# vulnerability
csa_nat_dpdp41 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp42 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# previous experience
csa_nat_dpdp51 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp52 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# committee chair
csa_nat_dpdp61 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp62 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# elected in midterm election
csa_nat_dpdp71 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp72 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# party ID (PJ and UCR only)
csa_nat_dpdp81 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='PJ'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp82 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='UCR'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# gender
csa_nat_dpdp91 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_nat_dpdp92 <- att_gt (yname='resignation_m100', gname='alternation30_nat_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# event-study version(s)
csa_nat_dpdp11_es <- aggte (csa_nat_dpdp11, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp12_es <- aggte (csa_nat_dpdp12, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp13_es <- aggte (csa_nat_dpdp13, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp21_es <- aggte (csa_nat_dpdp21, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp31_es <- aggte (csa_nat_dpdp31, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp32_es <- aggte (csa_nat_dpdp32, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp41_es <- aggte (csa_nat_dpdp41, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp42_es <- aggte (csa_nat_dpdp42, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp51_es <- aggte (csa_nat_dpdp51, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp52_es <- aggte (csa_nat_dpdp52, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp61_es <- aggte (csa_nat_dpdp61, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp62_es <- aggte (csa_nat_dpdp62, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp71_es <- aggte (csa_nat_dpdp71, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp72_es <- aggte (csa_nat_dpdp72, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp81_es <- aggte (csa_nat_dpdp81, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp82_es <- aggte (csa_nat_dpdp82, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp91_es <- aggte (csa_nat_dpdp91, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_nat_dpdp92_es <- aggte (csa_nat_dpdp92, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)

# ATTs across groups
csa_nat_dpdp11_gr <- aggte (csa_nat_dpdp11, type='group', na.rm=TRUE)
csa_nat_dpdp12_gr <- aggte (csa_nat_dpdp12, type='group', na.rm=TRUE)
csa_nat_dpdp13_gr <- aggte (csa_nat_dpdp13, type='group', na.rm=TRUE)
csa_nat_dpdp21_gr <- aggte (csa_nat_dpdp21, type='group', na.rm=TRUE)
csa_nat_dpdp31_gr <- aggte (csa_nat_dpdp31, type='group', na.rm=TRUE)
csa_nat_dpdp32_gr <- aggte (csa_nat_dpdp32, type='group', na.rm=TRUE)
csa_nat_dpdp41_gr <- aggte (csa_nat_dpdp41, type='group', na.rm=TRUE)
csa_nat_dpdp42_gr <- aggte (csa_nat_dpdp42, type='group', na.rm=TRUE)
csa_nat_dpdp51_gr <- aggte (csa_nat_dpdp51, type='group', na.rm=TRUE)
csa_nat_dpdp52_gr <- aggte (csa_nat_dpdp52, type='group', na.rm=TRUE)
csa_nat_dpdp61_gr <- aggte (csa_nat_dpdp61, type='group', na.rm=TRUE)
csa_nat_dpdp62_gr <- aggte (csa_nat_dpdp62, type='group', na.rm=TRUE)
csa_nat_dpdp71_gr <- aggte (csa_nat_dpdp71, type='group', na.rm=TRUE)
csa_nat_dpdp72_gr <- aggte (csa_nat_dpdp72, type='group', na.rm=TRUE)
csa_nat_dpdp81_gr <- aggte (csa_nat_dpdp81, type='group', na.rm=TRUE)
csa_nat_dpdp82_gr <- aggte (csa_nat_dpdp82, type='group', na.rm=TRUE)
csa_nat_dpdp91_gr <- aggte (csa_nat_dpdp91, type='group', na.rm=TRUE)
csa_nat_dpdp92_gr <- aggte (csa_nat_dpdp92, type='group', na.rm=TRUE)



#### (4.2.2) provincial alternation (within 30 days)

## (4.2.2.1) same party, same person

# no controls
csa_prov_spsp11 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp12 <- att_gt (yname='officeNext_elected100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp13 <- att_gt (yname='officeNext_appointed100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')


## splitting the sample(s) (main outcome only)

# top position in list
csa_prov_spsp31 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp32 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# vulnerability
csa_prov_spsp41 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp42 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# previous experience
csa_prov_spsp51 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp52 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# committee chair
csa_prov_spsp61 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp62 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# elected in midterm election
csa_prov_spsp71 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp72 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# party ID (PJ and UCR only)
csa_prov_spsp81 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='PJ'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp82 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='UCR'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# gender
csa_prov_spsp91 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spsp92 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spsp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# event-study version(s)
csa_prov_spsp11_es <- aggte (csa_prov_spsp11, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp12_es <- aggte (csa_prov_spsp12, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp13_es <- aggte (csa_prov_spsp13, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp31_es <- aggte (csa_prov_spsp31, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp32_es <- aggte (csa_prov_spsp32, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp41_es <- aggte (csa_prov_spsp41, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp42_es <- aggte (csa_prov_spsp42, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp51_es <- aggte (csa_prov_spsp51, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp52_es <- aggte (csa_prov_spsp52, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp61_es <- aggte (csa_prov_spsp61, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp62_es <- aggte (csa_prov_spsp62, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp71_es <- aggte (csa_prov_spsp71, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp72_es <- aggte (csa_prov_spsp72, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp81_es <- aggte (csa_prov_spsp81, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp82_es <- aggte (csa_prov_spsp82, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp91_es <- aggte (csa_prov_spsp91, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spsp92_es <- aggte (csa_prov_spsp92, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)

# ATTs across groups
csa_prov_spsp11_gr <- aggte (csa_prov_spsp11, type='group', na.rm=TRUE)
csa_prov_spsp12_gr <- aggte (csa_prov_spsp12, type='group', na.rm=TRUE)
csa_prov_spsp13_gr <- aggte (csa_prov_spsp13, type='group', na.rm=TRUE)
csa_prov_spsp31_gr <- aggte (csa_prov_spsp31, type='group', na.rm=TRUE)
csa_prov_spsp32_gr <- aggte (csa_prov_spsp32, type='group', na.rm=TRUE)
csa_prov_spsp41_gr <- aggte (csa_prov_spsp41, type='group', na.rm=TRUE)
csa_nat_spsp42_gr <- aggte (csa_nat_spsp42, type='group', na.rm=TRUE)
csa_nat_spsp51_gr <- aggte (csa_nat_spsp51, type='group', na.rm=TRUE)
csa_nat_spsp52_gr <- aggte (csa_nat_spsp52, type='group', na.rm=TRUE)
csa_nat_spsp61_gr <- aggte (csa_nat_spsp61, type='group', na.rm=TRUE)
csa_nat_spsp62_gr <- aggte (csa_nat_spsp62, type='group', na.rm=TRUE)
csa_nat_spsp71_gr <- aggte (csa_nat_spsp71, type='group', na.rm=TRUE)
csa_nat_spsp72_gr <- aggte (csa_nat_spsp72, type='group', na.rm=TRUE)
csa_nat_spsp81_gr <- aggte (csa_nat_spsp81, type='group', na.rm=TRUE)
csa_nat_spsp82_gr <- aggte (csa_nat_spsp82, type='group', na.rm=TRUE)
csa_nat_spsp91_gr <- aggte (csa_nat_spsp91, type='group', na.rm=TRUE)
csa_nat_spsp92_gr <- aggte (csa_nat_spsp92, type='group', na.rm=TRUE)


## (4.2.2.2) same party, different person

# no controls
csa_prov_spdp11 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp12 <- att_gt (yname='officeNext_elected100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp13 <- att_gt (yname='officeNext_appointed100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')


## splitting the sample(s) (main outcome only)

# top position in list
csa_prov_spdp31 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp32 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# vulnerability
csa_prov_spdp41 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp42 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# previous experience
csa_prov_spdp51 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp52 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# committee chair
csa_prov_spdp61 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp62 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# elected in midterm election
csa_prov_spdp71 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp72 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# party ID (PJ and UCR only)
csa_prov_spdp81 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='PJ'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp82 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='UCR'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# gender
csa_prov_spdp91 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_spdp92 <- att_gt (yname='resignation_m100', gname='alternation30_prov_spdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# event-study version(s)
csa_prov_spdp11_es <- aggte (csa_prov_spdp11, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp12_es <- aggte (csa_prov_spdp12, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp13_es <- aggte (csa_prov_spdp13, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp31_es <- aggte (csa_prov_spdp31, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp32_es <- aggte (csa_prov_spdp32, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp41_es <- aggte (csa_prov_spdp41, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp42_es <- aggte (csa_prov_spdp42, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp51_es <- aggte (csa_prov_spdp51, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp52_es <- aggte (csa_prov_spdp52, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp61_es <- aggte (csa_prov_spdp61, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp62_es <- aggte (csa_prov_spdp62, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp71_es <- aggte (csa_prov_spdp71, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp72_es <- aggte (csa_prov_spdp72, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp81_es <- aggte (csa_prov_spdp81, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp82_es <- aggte (csa_prov_spdp82, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp91_es <- aggte (csa_prov_spdp91, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_spdp92_es <- aggte (csa_prov_spdp92, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)

# ATTs across groups
csa_prov_spdp11_gr <- aggte (csa_prov_spdp11, type='group', na.rm=TRUE)
csa_prov_spdp12_gr <- aggte (csa_prov_spdp12, type='group', na.rm=TRUE)
csa_prov_spdp13_gr <- aggte (csa_prov_spdp13, type='group', na.rm=TRUE)
csa_prov_spdp31_gr <- aggte (csa_prov_spdp31, type='group', na.rm=TRUE)
csa_prov_spdp32_gr <- aggte (csa_prov_spdp32, type='group', na.rm=TRUE)
csa_prov_spdp41_gr <- aggte (csa_prov_spdp41, type='group', na.rm=TRUE)
csa_nat_spdp42_gr <- aggte (csa_nat_spdp42, type='group', na.rm=TRUE)
csa_nat_spdp51_gr <- aggte (csa_nat_spdp51, type='group', na.rm=TRUE)
csa_nat_spdp52_gr <- aggte (csa_nat_spdp52, type='group', na.rm=TRUE)
csa_nat_spdp61_gr <- aggte (csa_nat_spdp61, type='group', na.rm=TRUE)
csa_nat_spdp62_gr <- aggte (csa_nat_spdp62, type='group', na.rm=TRUE)
csa_nat_spdp71_gr <- aggte (csa_nat_spdp71, type='group', na.rm=TRUE)
csa_nat_spdp72_gr <- aggte (csa_nat_spdp72, type='group', na.rm=TRUE)
csa_nat_spdp81_gr <- aggte (csa_nat_spdp81, type='group', na.rm=TRUE)
csa_nat_spdp82_gr <- aggte (csa_nat_spdp82, type='group', na.rm=TRUE)
csa_nat_spdp91_gr <- aggte (csa_nat_spdp91, type='group', na.rm=TRUE)
csa_nat_spdp92_gr <- aggte (csa_nat_spdp92, type='group', na.rm=TRUE)


## (4.2.2.3) different party, different person

# no controls
csa_prov_dpdp11 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp12 <- att_gt (yname='officeNext_elected100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp13 <- att_gt (yname='officeNext_appointed100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=lmonth_csa, allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')


## splitting the sample(s) (main outcome only)

# top position in list
csa_prov_dpdp31 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp32 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  top01==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# vulnerability
csa_prov_dpdp41 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp42 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  vulnerable==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# previous experience
csa_prov_dpdp51 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp52 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  any.b==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# committee chair
csa_prov_dpdp61 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp62 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  chair==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# elected in midterm election
csa_prov_dpdp71 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp72 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  midterm==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# party ID (PJ and UCR only)
csa_prov_dpdp81 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='PJ'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp82 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  party=='UCR'), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# gender
csa_prov_dpdp91 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==0), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')
csa_prov_dpdp92 <- att_gt (yname='resignation_m100', gname='alternation30_prov_dpdp_month', idname='id', tname='month_id_period', xformla=~1, data=filter (lmonth_csa,  female==1), allow_unbalanced_panel=TRUE, control_group='notyettreated', bstrap=TRUE, biters=667, clustervars='name', est_method='dr')

# event-study version(s)
csa_prov_dpdp11_es <- aggte (csa_prov_dpdp11, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp12_es <- aggte (csa_prov_dpdp12, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp13_es <- aggte (csa_prov_dpdp13, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp31_es <- aggte (csa_prov_dpdp31, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp32_es <- aggte (csa_prov_dpdp32, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp41_es <- aggte (csa_prov_dpdp41, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp42_es <- aggte (csa_prov_dpdp42, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp51_es <- aggte (csa_prov_dpdp51, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp52_es <- aggte (csa_prov_dpdp52, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp61_es <- aggte (csa_prov_dpdp61, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp62_es <- aggte (csa_prov_dpdp62, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp71_es <- aggte (csa_prov_dpdp71, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp72_es <- aggte (csa_prov_dpdp72, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp81_es <- aggte (csa_prov_dpdp81, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp82_es <- aggte (csa_prov_dpdp82, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp91_es <- aggte (csa_prov_dpdp91, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)
csa_prov_dpdp92_es <- aggte (csa_prov_dpdp92, min_e=-5, max_e=5, type='dynamic', na.rm=TRUE)

# ATTs across groups
csa_prov_dpdp11_gr <- aggte (csa_prov_dpdp11, type='group', na.rm=TRUE)
csa_prov_dpdp12_gr <- aggte (csa_prov_dpdp12, type='group', na.rm=TRUE)
csa_prov_dpdp13_gr <- aggte (csa_prov_dpdp13, type='group', na.rm=TRUE)
csa_prov_dpdp31_gr <- aggte (csa_prov_dpdp31, type='group', na.rm=TRUE)
csa_prov_dpdp32_gr <- aggte (csa_prov_dpdp32, type='group', na.rm=TRUE)
csa_prov_dpdp41_gr <- aggte (csa_prov_dpdp41, type='group', na.rm=TRUE)
csa_nat_dpdp42_gr <- aggte (csa_nat_dpdp42, type='group', na.rm=TRUE)
csa_nat_dpdp51_gr <- aggte (csa_nat_dpdp51, type='group', na.rm=TRUE)
csa_nat_dpdp52_gr <- aggte (csa_nat_dpdp52, type='group', na.rm=TRUE)
csa_nat_dpdp61_gr <- aggte (csa_nat_dpdp61, type='group', na.rm=TRUE)
csa_nat_dpdp62_gr <- aggte (csa_nat_dpdp62, type='group', na.rm=TRUE)
csa_nat_dpdp71_gr <- aggte (csa_nat_dpdp71, type='group', na.rm=TRUE)
csa_nat_dpdp72_gr <- aggte (csa_nat_dpdp72, type='group', na.rm=TRUE)
csa_nat_dpdp81_gr <- aggte (csa_nat_dpdp81, type='group', na.rm=TRUE)
csa_nat_dpdp82_gr <- aggte (csa_nat_dpdp82, type='group', na.rm=TRUE)
csa_nat_dpdp91_gr <- aggte (csa_nat_dpdp91, type='group', na.rm=TRUE)
csa_nat_dpdp92_gr <- aggte (csa_nat_dpdp92, type='group', na.rm=TRUE)





#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 5 // REPORTING THE RESULTS ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

## setting a new WD
setwd ("/Users/adrianlucardi/Dropbox/Current Projects/Resignation/paper/Paper Resignations 10 - LSQ final/Figures/")



###### (5.1) Main results: TWFE, monthly data ######

##### (5.1.1) Main results: by Month, no interactions, full controls. All time windows ######

### (5.1.1.1) Table with results ######

## constructing a matrix with the values, which we will later format for Latex
(tab11coef <- bind_rows (
  
  # 30-day interval
  bind_cols (sum_mols30_all10b$coef[1:6,1], sum_mols30_all10c$coef[1:6,1], sum_mols30_all10d$coef[1:6,1], sum_surv30_all10b[2:7,1], rep (NA, 6)
             , sum_mols30_all20b$coef[1:6,1], sum_mols30_all20c$coef[1:6,1], sum_mols30_all20d$coef[1:6,1], sum_surv30_all20b[2:7,1], rep (NA, 6)
             , sum_mols30_all30b$coef[1:6,1], sum_mols30_all30c$coef[1:6,1], sum_mols30_all30d$coef[1:6,1], sum_surv30_all30b[2:7,1])
  
  # 60-day interval
  , bind_cols (sum_mols60_all10b$coef[1:6,1], sum_mols60_all10c$coef[1:6,1], sum_mols60_all10d$coef[1:6,1], sum_surv60_all10b[2:7,1], rep (NA, 6)
             , sum_mols60_all20b$coef[1:6,1], sum_mols60_all20c$coef[1:6,1], sum_mols60_all20d$coef[1:6,1], sum_surv60_all20b[2:7,1], rep (NA, 6)
             , sum_mols60_all30b$coef[1:6,1], sum_mols60_all30c$coef[1:6,1], sum_mols60_all30d$coef[1:6,1], sum_surv60_all30b[2:7,1])
  
  # 90-day interval
  , bind_cols (sum_mols90_all10b$coef[1:6,1], sum_mols90_all10c$coef[1:6,1], sum_mols90_all10d$coef[1:6,1], sum_surv90_all10b[2:7,1], rep (NA, 6)
             , sum_mols90_all20b$coef[1:6,1], sum_mols90_all20c$coef[1:6,1], sum_mols90_all20d$coef[1:6,1], sum_surv90_all20b[2:7,1], rep (NA, 6)
             , sum_mols90_all30b$coef[1:6,1], sum_mols90_all30c$coef[1:6,1], sum_mols90_all30d$coef[1:6,1], sum_surv90_all30b[2:7,1]) ))
(tab11se <- bind_rows (
  
  # 30-day interval
  bind_cols (sum_mols30_all10b$coef[1:6,2], sum_mols30_all10c$coef[1:6,2], sum_mols30_all10d$coef[1:6,2], sum_surv30_all10b[2:7,2], rep (NA, 6)
             , sum_mols30_all20b$coef[1:6,2], sum_mols30_all20c$coef[1:6,2], sum_mols30_all20d$coef[1:6,2], sum_surv30_all20b[2:7,2], rep (NA, 6)
             , sum_mols30_all30b$coef[1:6,2], sum_mols30_all30c$coef[1:6,2], sum_mols30_all30d$coef[1:6,2], sum_surv30_all30b[2:7,2])
  
  # 60-day interval
  , bind_cols (sum_mols60_all10b$coef[1:6,2], sum_mols60_all10c$coef[1:6,2], sum_mols60_all10d$coef[1:6,2], sum_surv60_all10b[2:7,2], rep (NA, 6)
               , sum_mols60_all20b$coef[1:6,2], sum_mols60_all20c$coef[1:6,2], sum_mols60_all20d$coef[1:6,2], sum_surv60_all20b[2:7,2], rep (NA, 6)
               , sum_mols60_all30b$coef[1:6,2], sum_mols60_all30c$coef[1:6,2], sum_mols60_all30d$coef[1:6,2], sum_surv60_all30b[2:7,2])
  
  # 90-day interval
  , bind_cols (sum_mols90_all10b$coef[1:6,2], sum_mols90_all10c$coef[1:6,2], sum_mols90_all10d$coef[1:6,2], sum_surv90_all10b[2:7,2], rep (NA, 6)
               , sum_mols90_all20b$coef[1:6,2], sum_mols90_all20c$coef[1:6,2], sum_mols90_all20d$coef[1:6,2], sum_surv90_all20b[2:7,2], rep (NA, 6)
               , sum_mols90_all30b$coef[1:6,2], sum_mols90_all30c$coef[1:6,2], sum_mols90_all30d$coef[1:6,2], sum_surv90_all30b[2:7,2]) ))

# creating the table
(tab11all <- parSE (bind_rows (tab11coef, tab11se)[sort (rep (1:18, 2)) + rep (c (0, 18), 2),], n.digit=digits))

# formatting for Latex
rows <- rep (c ("national:", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
                , "national:", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
                , "national:", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
                , "[0.50ex] provincial:", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
                , "provincial:", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
                , "provincial:", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"), 3)
(tab11ns <- c ( sum (mols30_all10b$response/100), sum (mols30_all20b$response/100), sum (mols30_all30b$response/100)))

Header1 <- paste ("\\toprule & \\multicolumn{4}{c}{\\emph{resignation}} & & \\multicolumn{4}{c}{\\emph{resignation \\& election}} & & \\multicolumn{4}{c}{\\emph{resignation \\& app'tment}} \\\\ \\cmidrule{2-5} \\cmidrule{7-10} \\cmidrule{12-15} \n")
Header2 <- paste ("\\multicolumn{1}{l}{(a) 30-day window} & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} & & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & & \\multicolumn{1}{c}{(9)} & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} \\\\ \\midrule \n")
Header3 <- paste ("[1.25ex] \\multicolumn{15}{l}{(b) 60-day window} \\\\ \\midrule \n")
Header4 <- paste ("[1.25ex] \\multicolumn{15}{l}{(c) 90-day window} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule specification & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{glm}} & & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{glm}} & & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{ols}} & \\multicolumn{1}{c}{\\textsc{glm}} \\\\ \n")
add.n03 <- paste ("[0.20ex] legislator \\textsc{fe}'s & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} \\\\ \n")
add.n04 <- paste ("[0.20ex] leg.-mandate \\textsc{fe}'s & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} \\\\ \n")

add.n05 <- paste ("\\midrule $\\sum Y = 1$ & \\multicolumn{4}{c}{", sprintf ("%.0f", round (tab11ns[1],0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (tab11ns[2],0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (tab11ns[3],0)), "} \\\\ \n")
add.n06 <- paste ("[0.20ex] num. legislators & \\multicolumn{4}{c}{", sprintf ("%.0f", round (length (unique (lmonth3$name)),0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (length (unique (lmonth3$name)),0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (length (unique (lmonth3$name)),0)), "} \\\\ \n")
add.n07 <- paste ("[0.20ex] num. mandates & \\multicolumn{4}{c}{", sprintf ("%.0f", round (length (unique (lmonth3$id)),0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (length (unique (lmonth3$id)),0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (length (unique (lmonth3$id)),0)), "} \\\\ \n")
add.n08 <- paste ("[0.20ex] num. obs. & \\multicolumn{4}{c}{", sprintf ("%.0f", round (mols30_all10b$N,0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (mols30_all20b$N,0)), "} & & \\multicolumn{4}{c}{", sprintf ("%.0f", round (mols30_all30b$N,0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 12
addtorow$pos[[4]] <- 24
addtorow$pos[[5]] <- 36
addtorow$pos[[6]] <- 36
addtorow$pos[[7]] <- 36
addtorow$pos[[8]] <- 36
addtorow$command <- c (Header1, Header2, Header3, Header4
                       , add.n01, add.n03, add.n04, add.n05)
print (xtable ( cbind (rows, tab11all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017"
                , label="T:resultsMain")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lmonth3, month_id_period >= 4)$name)));round (length (unique (filter (lmonth3, month_id_period >= 4)$id))); sum_mols90_all10b$N
round (length (unique (filter (lmonth3, month_id_period >= 3)$name)));round (length (unique (filter (lmonth3, month_id_period >= 3)$id))); sum_mols60_all10b$N
round (length (unique (filter (lmonth3, month_id_period >= 2)$name)));round (length (unique (filter (lmonth3, month_id_period >= 2)$id))); sum_mols30_all10b$N



#### (5.1.1.2) Visualization ######

## getting the values of interest
tab11plot <- bind_cols (
  rep (c (rep ('alternation at the national level', 3), rep ('alternation at the provincial level', 3)), 3)
  , rep (c ('same party,\nsame person', 'same party,\ndifferent person', 'different party,\ndifferent person'), 6)
  , c (rep ('resignation (0/100)', 6), rep ('resignation &\nelection (0/100)', 6), rep ('resignation &\nappointment (0/100)', 6))
  , unlist (tab11coef[1:6,c (1,6,11)])
  , unlist (tab11se[1:6,c (1,6,11)])
  ) %>% setNames (., c ('level', 'treat', 'outcome', 'estim', 'se')
                  ) %>% mutate (
                    level = factor (level, levels=c ('alternation at the national level', 'alternation at the provincial level'))
                    , treat = factor (treat, levels=c ('different party,\ndifferent person', 'same party,\ndifferent person', 'same party,\nsame person'))
                    , outcome = factor (outcome, levels=c ('resignation (0/100)', 'resignation &\nelection (0/100)', 'resignation &\nappointment (0/100)'))
                    , crit_val = qt (0.975, df=length (unique (lmonth3$name)))
                    , low95 = estim - se*crit_val
                    , high95 = estim + se*crit_val)
summary (tab11plot)


## drawing the plot
(plot11_full <- ggplot (tab11plot
                        , aes (x=estim, y=treat, color=level))
  + geom_vline (xintercept=0, linetype=3)
  + geom_point (position=position_dodge (width=0.5))
  + geom_errorbar (aes (xmin=low95, xmax=high95), position=position_dodge (width=0.5), width=0.125)
  + facet_grid (~ outcome)
  + scale_color_manual (values=movie)
  + scale_x_continuous (breaks=-1:5)
  + ylab ('') + xlab ('estimated effect')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-10,-9,-9,-9)) )


## exporting

pwid <- 5.5
phei <- 2.5

ggsave ("figMain30.png"
        , plot11_full, width=pwid, height=phei, units="in", dpi=600)




##### (5.1.2) Main interaction: by Month, 30-day window, only OLS models ######

#### (5.1.2.1) Table ######

## constructing a matrix with the values, which we will later format for Latex
(tab12coef <- bind_cols (
    sum_mols30_all11b$coef[1:12,1], sum_mols30_all21b$coef[1:12,1], sum_mols30_all31b$coef[1:12,1], rep (NA, 12)
    , sum_mols30_all12b$coef[1:12,1], sum_mols30_all22b$coef[1:12,1], sum_mols30_all32b$coef[1:12,1], rep (NA, 12)
    , sum_mols30_all13b$coef[1:12,1], sum_mols30_all23b$coef[1:12,1], sum_mols30_all33b$coef[1:12,1], rep (NA, 12)
    , sum_mols30_all14b$coef[1:12,1], sum_mols30_all24b$coef[1:12,1], sum_mols30_all34b$coef[1:12,1], rep (NA, 12)
    , sum_mols30_all15b$coef[1:12,1], sum_mols30_all25b$coef[1:12,1], sum_mols30_all35b$coef[1:12,1], rep (NA, 12)
    , sum_mols30_all16b$coef[1:12,1], sum_mols30_all26b$coef[1:12,1], sum_mols30_all36b$coef[1:12,1], rep (NA, 12)
    , sum_mols30_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols30_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols30_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1]))
(tab12se <- bind_cols (
  sum_mols30_all11b$coef[1:12,2], sum_mols30_all21b$coef[1:12,2], sum_mols30_all31b$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all12b$coef[1:12,2], sum_mols30_all22b$coef[1:12,2], sum_mols30_all32b$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all13b$coef[1:12,2], sum_mols30_all23b$coef[1:12,2], sum_mols30_all33b$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all14b$coef[1:12,2], sum_mols30_all24b$coef[1:12,2], sum_mols30_all34b$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all15b$coef[1:12,2], sum_mols30_all25b$coef[1:12,2], sum_mols30_all35b$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all16b$coef[1:12,2], sum_mols30_all26b$coef[1:12,2], sum_mols30_all36b$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols30_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols30_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2]))

## creating the table
(tab12all <- parSE (bind_rows (tab12coef, tab12se)[sort (rep (1:12, 2)) + rep (c (0, 12), 2),], n.digit=digits))

## formatting for Latex
rows <- c ("national (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "[1.5ex] provincial (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , "")

(tab12ns <- c (sum (mols30_all11b$response/100), sum (mols30_all21b$response/100), sum (mols30_all31b$response/100), sum (mols30_all12b$response/100), sum (mols30_all22b$response/100), sum (mols30_all32b$response/100), sum (mols30_all13b$response/100), sum (mols30_all23b$response/100), sum (mols30_all33b$response/100), sum (mols30_all14b$response/100), sum (mols30_all24b$response/100), sum (mols30_all34b$response/100), sum (mols30_all15b$response/100), sum (mols30_all25b$response/100), sum (mols30_all35b$response/100), sum (mols30_all16b$response/100), sum (mols30_all26b$response/100), sum (mols30_all36b$response/100), sum (mols30_all17b$response/100), sum (mols30_all27b$response/100), sum (mols30_all37b$response/100) ))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{position in list}} & & \\multicolumn{3}{c}{\\textbf{elect. vulnerable}} & & \\multicolumn{3}{c}{\\textbf{previous experience}} & & \\multicolumn{3}{c}{\\textbf{committee chair}} & & \\multicolumn{3}{c}{\\textbf{elected in midterm}} & & \\multicolumn{3}{c}{\\textbf{female}} & & \\multicolumn{3}{c}{\\textbf{party (\\textsc{ucr}/\\textsc{pj})}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \\cmidrule{14-16} \\cmidrule{18-20} \\cmidrule{22-24} \\cmidrule{26-28} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} & & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} & & \\multicolumn{1}{c}{(13)} & \\multicolumn{1}{c}{(14)} & \\multicolumn{1}{c}{(15)} & & \\multicolumn{1}{c}{(16)} & \\multicolumn{1}{c}{(17)} & \\multicolumn{1}{c}{(18)} & & \\multicolumn{1}{c}{(19)} & \\multicolumn{1}{c}{(20)} & \\multicolumn{1}{c}{(21)} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule outcome & \\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} \\\\ \n")
add.n02 <- paste ("$\\sum Y = 1$ & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[9],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[10],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[11],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[12],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[13],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[14],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[15],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[16],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[17],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[18],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[19],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[20],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab12ns[21],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 24
addtorow$pos[[4]] <- 24
addtorow$command <- c (Header1, Header2, add.n01, add.n02)
print (xtable ( cbind (rows, tab12all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017: Conditional effects"
                , label="T:resultsInt30")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lmonth3, month_id_period >= 2)$name)));round (length (unique (filter (lmonth3, month_id_period >= 2)$id))); mols30_all10b$N




#### (5.1.2.2) Visualization ######

## getting the values of interest
tab12plot <- bind_cols (
  rep (c (rep ('alternation at the\nnational level', 2), rep ('alternation at the\nprovincial level', 2)), 21)
  , c (rep ('position in\nparty list (0:1)', 12)
       , rep ('electorally\nvulnerable (0/1)', 12)
       , rep ('previous\nexperience (0/1)', 12)
       , rep ('committee\nchair (0/1)', 12)
       , rep ('elected in\nmidterm (0/1)', 12)
       , rep ('female (0/1)', 12)
       , rep ('party ID:\nUCR (0) vs PJ (1)', 12))
  , rep (c (rep ('resignation (0/100)', 4), rep ('resignation &\nelection (0/100)', 4), rep ('resignation &\nappointment (0/100)', 4)), 7)
  , rep (c ('baseline\n(cond. variable = 0)', 'interaction\n(cond. variable = 1)'), 42)
  , unlist (tab12coef[c (5:6, 11:12),c(1:3,5:7,9:11,13:15,17:19,21:23,25:27)])
  , unlist (tab12se[c (5:6, 11:12),c(1:3,5:7,9:11,13:15,17:19,21:23,25:27)])
) %>% setNames (., c ('level', 'cond_var', 'outcome', 'base_cond' , 'estim', 'se')
) %>% mutate (
  level = factor (level, levels=c ('alternation at the\nnational level', 'alternation at the\nprovincial level'))
  , cond_var = factor (cond_var, levels=c (
    'party ID:\nUCR (0) vs PJ (1)'
    , 'female (0/1)'
    , 'elected in\nmidterm (0/1)'
    , 'committee\nchair (0/1)'
    , 'previous\nexperience (0/1)'
    , 'electorally\nvulnerable (0/1)'
    , 'position in\nparty list (0:1)'))
  , outcome = factor (outcome, levels=c ('resignation (0/100)', 'resignation &\nelection (0/100)', 'resignation &\nappointment (0/100)'))
  , base_cond = factor (base_cond, levels=c ('interaction\n(cond. variable = 1)', 'baseline\n(cond. variable = 0)'))
  , crit_val = qt (0.975, df=length (unique (lmonth3$name)))
  , low95 = estim - se*crit_val
  , high95 = estim + se*crit_val)
summary (tab12plot)


## drawing the plot
(plot12_full <- ggplot (
  tab12plot, aes (x=estim, y=cond_var, color=level, shape=base_cond, linetype=base_cond))
  + geom_vline (xintercept=0, linetype=3)
  + geom_point (position=position_dodge (width=0.5))
  + geom_errorbar (aes (xmin=low95, xmax=high95), position=position_dodge (width=0.5), width=0.125)
  + facet_grid (~ outcome)
  + scale_color_manual (values=movie)
  + ylab ('') + xlab ('estimated effect')
  + coord_cartesian (xlim=c (-9.5, 13.5))
  + scale_x_continuous (breaks=seq (-9, 12, by=3), labels=seq (-9, 12, by=3))
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-10,-9,-9,-9)) )


## exporting
pwid <- 6
phei <- 2.5*2

ggsave ("figInteractions30.png"
        , plot12_full, width=pwid, height=phei, units="in", dpi=600)




##### (5.1.3) Main interaction: by Month, 60-day window, only OLS models ######

## constructing a matrix with the values, which we will later format for Latex
(tab13coef <- bind_cols (
  sum_mols60_all11b$coef[1:12,1], sum_mols60_all21b$coef[1:12,1], sum_mols60_all31b$coef[1:12,1], rep (NA, 12)
  , sum_mols60_all12b$coef[1:12,1], sum_mols60_all22b$coef[1:12,1], sum_mols60_all32b$coef[1:12,1], rep (NA, 12)
  , sum_mols60_all13b$coef[1:12,1], sum_mols60_all23b$coef[1:12,1], sum_mols60_all33b$coef[1:12,1], rep (NA, 12)
  , sum_mols60_all14b$coef[1:12,1], sum_mols60_all24b$coef[1:12,1], sum_mols60_all34b$coef[1:12,1], rep (NA, 12)
  , sum_mols60_all15b$coef[1:12,1], sum_mols60_all25b$coef[1:12,1], sum_mols60_all35b$coef[1:12,1], rep (NA, 12)
  , sum_mols60_all16b$coef[1:12,1], sum_mols60_all26b$coef[1:12,1], sum_mols60_all36b$coef[1:12,1], rep (NA, 12)
  , sum_mols60_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols60_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols60_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1]))
(tab13se <- bind_cols (
  sum_mols60_all11b$coef[1:12,2], sum_mols60_all21b$coef[1:12,2], sum_mols60_all31b$coef[1:12,2], rep (NA, 12)
  , sum_mols60_all12b$coef[1:12,2], sum_mols60_all22b$coef[1:12,2], sum_mols60_all32b$coef[1:12,2], rep (NA, 12)
  , sum_mols60_all13b$coef[1:12,2], sum_mols60_all23b$coef[1:12,2], sum_mols60_all33b$coef[1:12,2], rep (NA, 12)
  , sum_mols60_all14b$coef[1:12,2], sum_mols60_all24b$coef[1:12,2], sum_mols60_all34b$coef[1:12,2], rep (NA, 12)
  , sum_mols60_all15b$coef[1:12,2], sum_mols60_all25b$coef[1:12,2], sum_mols60_all35b$coef[1:12,2], rep (NA, 12)
  , sum_mols60_all16b$coef[1:12,2], sum_mols60_all26b$coef[1:12,2], sum_mols60_all36b$coef[1:12,2], rep (NA, 12)
  , sum_mols60_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols60_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols60_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2]))

## creating the table
(tab13all <- parSE (bind_rows (tab13coef, tab13se)[sort (rep (1:12, 2)) + rep (c (0, 12), 2),], n.digit=digits))

## formatting for Latex
rows <- c ("national (60-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (60-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (60-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "[1.5ex] provincial (60-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (60-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (60-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , "")

(tab13ns <- c (sum (mols60_all11b$response/100), sum (mols60_all21b$response/100), sum (mols60_all31b$response/100), sum (mols60_all12b$response/100), sum (mols60_all22b$response/100), sum (mols60_all32b$response/100), sum (mols60_all13b$response/100), sum (mols60_all23b$response/100), sum (mols60_all33b$response/100), sum (mols60_all14b$response/100), sum (mols60_all24b$response/100), sum (mols60_all34b$response/100), sum (mols60_all15b$response/100), sum (mols60_all25b$response/100), sum (mols60_all35b$response/100), sum (mols60_all16b$response/100), sum (mols60_all26b$response/100), sum (mols60_all36b$response/100), sum (mols60_all17b$response/100), sum (mols60_all27b$response/100), sum (mols60_all37b$response/100) ))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{position in list}} & & \\multicolumn{3}{c}{\\textbf{elect. vulnerable}} & & \\multicolumn{3}{c}{\\textbf{previous experience}} & & \\multicolumn{3}{c}{\\textbf{committee chair}} & & \\multicolumn{3}{c}{\\textbf{elected in midterm}} & & \\multicolumn{3}{c}{\\textbf{female}} & & \\multicolumn{3}{c}{\\textbf{party (\\textsc{ucr}/\\textsc{pj})}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \\cmidrule{14-16} \\cmidrule{18-20} \\cmidrule{22-24} \\cmidrule{26-28} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} & & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} & & \\multicolumn{1}{c}{(13)} & \\multicolumn{1}{c}{(14)} & \\multicolumn{1}{c}{(15)} & & \\multicolumn{1}{c}{(16)} & \\multicolumn{1}{c}{(17)} & \\multicolumn{1}{c}{(18)} & & \\multicolumn{1}{c}{(19)} & \\multicolumn{1}{c}{(20)} & \\multicolumn{1}{c}{(21)} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule outcome & \\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} \\\\ \n")
add.n02 <- paste ("$\\sum Y = 1$ & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[9],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[10],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[11],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[12],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[13],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[14],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[15],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[16],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[17],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[18],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[19],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[20],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab13ns[21],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 24
addtorow$pos[[4]] <- 24
addtorow$command <- c (Header1, Header2, add.n01, add.n02)
print (xtable ( cbind (rows, tab13all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017: Conditional effects (60-day window)"
                , label="T:resultsInt60")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lmonth3, month_id_period >= 3)$name)));round (length (unique (filter (lmonth3, month_id_period >= 3)$id))); mols60_all10b$N



#### (5.1.4) Main interaction: by Month, 90-day window, only OLS models ######

## constructing a matrix with the values, which we will later format for Latex
(tab14coef <- bind_cols (
  sum_mols90_all11b$coef[1:12,1], sum_mols90_all21b$coef[1:12,1], sum_mols90_all31b$coef[1:12,1], rep (NA, 12)
  , sum_mols90_all12b$coef[1:12,1], sum_mols90_all22b$coef[1:12,1], sum_mols90_all32b$coef[1:12,1], rep (NA, 12)
  , sum_mols90_all13b$coef[1:12,1], sum_mols90_all23b$coef[1:12,1], sum_mols90_all33b$coef[1:12,1], rep (NA, 12)
  , sum_mols90_all14b$coef[1:12,1], sum_mols90_all24b$coef[1:12,1], sum_mols90_all34b$coef[1:12,1], rep (NA, 12)
  , sum_mols90_all15b$coef[1:12,1], sum_mols90_all25b$coef[1:12,1], sum_mols90_all35b$coef[1:12,1], rep (NA, 12)
  , sum_mols90_all16b$coef[1:12,1], sum_mols90_all26b$coef[1:12,1], sum_mols90_all36b$coef[1:12,1], rep (NA, 12)
  , sum_mols90_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols90_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols90_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1]))
(tab14se <- bind_cols (
  sum_mols90_all11b$coef[1:12,2], sum_mols90_all21b$coef[1:12,2], sum_mols90_all31b$coef[1:12,2], rep (NA, 12)
  , sum_mols90_all12b$coef[1:12,2], sum_mols90_all22b$coef[1:12,2], sum_mols90_all32b$coef[1:12,2], rep (NA, 12)
  , sum_mols90_all13b$coef[1:12,2], sum_mols90_all23b$coef[1:12,2], sum_mols90_all33b$coef[1:12,2], rep (NA, 12)
  , sum_mols90_all14b$coef[1:12,2], sum_mols90_all24b$coef[1:12,2], sum_mols90_all34b$coef[1:12,2], rep (NA, 12)
  , sum_mols90_all15b$coef[1:12,2], sum_mols90_all25b$coef[1:12,2], sum_mols90_all35b$coef[1:12,2], rep (NA, 12)
  , sum_mols90_all16b$coef[1:12,2], sum_mols90_all26b$coef[1:12,2], sum_mols90_all36b$coef[1:12,2], rep (NA, 12)
  , sum_mols90_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols90_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols90_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2]))

## creating the table
(tab14all <- parSE (bind_rows (tab14coef, tab14se)[sort (rep (1:12, 2)) + rep (c (0, 12), 2),], n.digit=digits))

## formatting for Latex
rows <- c ("national (90-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (90-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (90-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "[1.5ex] provincial (90-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (90-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (90-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , "")

(tab14ns <- c (sum (mols90_all11b$response/100), sum (mols90_all21b$response/100), sum (mols90_all31b$response/100), sum (mols90_all12b$response/100), sum (mols90_all22b$response/100), sum (mols90_all32b$response/100), sum (mols90_all13b$response/100), sum (mols90_all23b$response/100), sum (mols90_all33b$response/100), sum (mols90_all14b$response/100), sum (mols90_all24b$response/100), sum (mols90_all34b$response/100), sum (mols90_all15b$response/100), sum (mols90_all25b$response/100), sum (mols90_all35b$response/100), sum (mols90_all16b$response/100), sum (mols90_all26b$response/100), sum (mols90_all36b$response/100), sum (mols90_all17b$response/100), sum (mols90_all27b$response/100), sum (mols90_all37b$response/100) ))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{position in list}} & & \\multicolumn{3}{c}{\\textbf{elect. vulnerable}} & & \\multicolumn{3}{c}{\\textbf{previous experience}} & & \\multicolumn{3}{c}{\\textbf{committee chair}} & & \\multicolumn{3}{c}{\\textbf{elected in midterm}} & & \\multicolumn{3}{c}{\\textbf{female}} & & \\multicolumn{3}{c}{\\textbf{party (\\textsc{ucr}/\\textsc{pj})}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \\cmidrule{14-16} \\cmidrule{18-20} \\cmidrule{22-24} \\cmidrule{26-28} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} & & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} & & \\multicolumn{1}{c}{(13)} & \\multicolumn{1}{c}{(14)} & \\multicolumn{1}{c}{(15)} & & \\multicolumn{1}{c}{(16)} & \\multicolumn{1}{c}{(17)} & \\multicolumn{1}{c}{(18)} & & \\multicolumn{1}{c}{(19)} & \\multicolumn{1}{c}{(20)} & \\multicolumn{1}{c}{(21)} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule outcome & \\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} \\\\ \n")
add.n02 <- paste ("$\\sum Y = 1$ & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[9],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[10],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[11],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[12],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[13],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[14],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[15],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[16],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[17],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[18],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[19],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[20],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab14ns[21],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 24
addtorow$pos[[4]] <- 24
addtorow$command <- c (Header1, Header2, add.n01, add.n02)
print (xtable ( cbind (rows, tab14all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017: Conditional effects (90-day window)"
                , label="T:resultsInt90")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lmonth3, month_id_period >= 4)$name)));round (length (unique (filter (lmonth3, month_id_period >= 4)$id))); mols90_all10b$N



#### (5.1.5) Main interaction: by Month, 30-day window, only OLS models, legislator fixed effects ######

## constructing a matrix with the values, which we will later format for Latex
(tab15coef <- bind_cols (
  sum_mols30_all11c$coef[1:12,1], sum_mols30_all21c$coef[1:12,1], sum_mols30_all31c$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all12c$coef[1:12,1], sum_mols30_all22c$coef[1:12,1], sum_mols30_all32c$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all13c$coef[1:12,1], sum_mols30_all23c$coef[1:12,1], sum_mols30_all33c$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all14c$coef[1:12,1], sum_mols30_all24c$coef[1:12,1], sum_mols30_all34c$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all15c$coef[1:12,1], sum_mols30_all25c$coef[1:12,1], sum_mols30_all35c$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all16c$coef[1:12,1], sum_mols30_all26c$coef[1:12,1], sum_mols30_all36c$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all17c$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols30_all27c$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols30_all37c$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1]))
(tab15se <- bind_cols (
  sum_mols30_all11c$coef[1:12,2], sum_mols30_all21c$coef[1:12,2], sum_mols30_all31c$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all12c$coef[1:12,2], sum_mols30_all22c$coef[1:12,2], sum_mols30_all32c$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all13c$coef[1:12,2], sum_mols30_all23c$coef[1:12,2], sum_mols30_all33c$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all14c$coef[1:12,2], sum_mols30_all24c$coef[1:12,2], sum_mols30_all34c$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all15c$coef[1:12,2], sum_mols30_all25c$coef[1:12,2], sum_mols30_all35c$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all16c$coef[1:12,2], sum_mols30_all26c$coef[1:12,2], sum_mols30_all36c$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all17c$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols30_all27c$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols30_all37c$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2]))

## creating the table
(tab15all <- parSE (bind_rows (tab15coef, tab15se)[sort (rep (1:12, 2)) + rep (c (0, 12), 2),], n.digit=digits))

## formatting for Latex
rows <- c ("national (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "[1.5ex] provincial (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , "")

(tab15ns <- c (sum (mols30_all11b$response/100), sum (mols30_all21b$response/100), sum (mols30_all31b$response/100), sum (mols30_all12b$response/100), sum (mols30_all22b$response/100), sum (mols30_all32b$response/100), sum (mols30_all13b$response/100), sum (mols30_all23b$response/100), sum (mols30_all33b$response/100), sum (mols30_all14b$response/100), sum (mols30_all24b$response/100), sum (mols30_all34b$response/100), sum (mols30_all15b$response/100), sum (mols30_all25b$response/100), sum (mols30_all35b$response/100), sum (mols30_all16b$response/100), sum (mols30_all26b$response/100), sum (mols30_all36b$response/100), sum (mols30_all17b$response/100), sum (mols30_all27b$response/100), sum (mols30_all37b$response/100) ))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{position in list}} & & \\multicolumn{3}{c}{\\textbf{elect. vulnerable}} & & \\multicolumn{3}{c}{\\textbf{previous experience}} & & \\multicolumn{3}{c}{\\textbf{committee chair}} & & \\multicolumn{3}{c}{\\textbf{elected in midterm}} & & \\multicolumn{3}{c}{\\textbf{female}} & & \\multicolumn{3}{c}{\\textbf{party (\\textsc{ucr}/\\textsc{pj})}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \\cmidrule{14-16} \\cmidrule{18-20} \\cmidrule{22-24} \\cmidrule{26-28} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} & & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} & & \\multicolumn{1}{c}{(13)} & \\multicolumn{1}{c}{(14)} & \\multicolumn{1}{c}{(15)} & & \\multicolumn{1}{c}{(16)} & \\multicolumn{1}{c}{(17)} & \\multicolumn{1}{c}{(18)} & & \\multicolumn{1}{c}{(19)} & \\multicolumn{1}{c}{(20)} & \\multicolumn{1}{c}{(21)} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule outcome & \\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} \\\\ \n")
add.n02 <- paste ("$\\sum Y = 1$ & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[9],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[10],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[11],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[12],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[13],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[14],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[15],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[16],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[17],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[18],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[19],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[20],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab15ns[21],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 24
addtorow$pos[[4]] <- 24
addtorow$command <- c (Header1, Header2, add.n01, add.n02)
print (xtable ( cbind (rows, tab15all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017: Conditional effects (30-day window with legislator fixed effects)"
                , label="T:resultsInt30legFE")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lmonth3, month_id_period >= 2)$name)));round (length (unique (filter (lmonth3, month_id_period >= 2)$id))); mols30_all11c$N



#### (5.1.6) Main interaction: by Month, 30-day window, only OLS models, legislator-mandate fixed effects ######

## constructing a matrix with the values, which we will later format for Latex
(tab16coef <- bind_cols (
  sum_mols30_all11d$coef[1:12,1], sum_mols30_all21d$coef[1:12,1], sum_mols30_all31d$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all12d$coef[1:12,1], sum_mols30_all22d$coef[1:12,1], sum_mols30_all32d$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all13d$coef[1:12,1], sum_mols30_all23d$coef[1:12,1], sum_mols30_all33d$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all14d$coef[1:12,1], sum_mols30_all24d$coef[1:12,1], sum_mols30_all34d$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all15d$coef[1:12,1], sum_mols30_all25d$coef[1:12,1], sum_mols30_all35d$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all16d$coef[1:12,1], sum_mols30_all26d$coef[1:12,1], sum_mols30_all36d$coef[1:12,1], rep (NA, 12)
  , sum_mols30_all17d$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols30_all27d$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_mols30_all37d$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1]))
(tab16se <- bind_cols (
  sum_mols30_all11d$coef[1:12,2], sum_mols30_all21d$coef[1:12,2], sum_mols30_all31d$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all12d$coef[1:12,2], sum_mols30_all22d$coef[1:12,2], sum_mols30_all32d$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all13d$coef[1:12,2], sum_mols30_all23d$coef[1:12,2], sum_mols30_all33d$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all14d$coef[1:12,2], sum_mols30_all24d$coef[1:12,2], sum_mols30_all34d$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all15d$coef[1:12,2], sum_mols30_all25d$coef[1:12,2], sum_mols30_all35d$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all16d$coef[1:12,2], sum_mols30_all26d$coef[1:12,2], sum_mols30_all36d$coef[1:12,2], rep (NA, 12)
  , sum_mols30_all17d$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols30_all27d$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_mols30_all37d$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2]))

## creating the table
(tab16all <- parSE (bind_rows (tab16coef, tab16se)[sort (rep (1:12, 2)) + rep (c (0, 12), 2),], n.digit=digits))

## formatting for Latex
rows <- c ("national (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "[1.5ex] provincial (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , "")

(tab16ns <- c (sum (mols30_all11b$response/100), sum (mols30_all21b$response/100), sum (mols30_all31b$response/100), sum (mols30_all12b$response/100), sum (mols30_all22b$response/100), sum (mols30_all32b$response/100), sum (mols30_all13b$response/100), sum (mols30_all23b$response/100), sum (mols30_all33b$response/100), sum (mols30_all14b$response/100), sum (mols30_all24b$response/100), sum (mols30_all34b$response/100), sum (mols30_all15b$response/100), sum (mols30_all25b$response/100), sum (mols30_all35b$response/100), sum (mols30_all16b$response/100), sum (mols30_all26b$response/100), sum (mols30_all36b$response/100), sum (mols30_all17b$response/100), sum (mols30_all27b$response/100), sum (mols30_all37b$response/100) ))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{position in list}} & & \\multicolumn{3}{c}{\\textbf{elect. vulnerable}} & & \\multicolumn{3}{c}{\\textbf{previous experience}} & & \\multicolumn{3}{c}{\\textbf{committee chair}} & & \\multicolumn{3}{c}{\\textbf{elected in midterm}} & & \\multicolumn{3}{c}{\\textbf{female}} & & \\multicolumn{3}{c}{\\textbf{party (\\textsc{ucr}/\\textsc{pj})}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \\cmidrule{14-16} \\cmidrule{18-20} \\cmidrule{22-24} \\cmidrule{26-28} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} & & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} & & \\multicolumn{1}{c}{(13)} & \\multicolumn{1}{c}{(14)} & \\multicolumn{1}{c}{(15)} & & \\multicolumn{1}{c}{(16)} & \\multicolumn{1}{c}{(17)} & \\multicolumn{1}{c}{(18)} & & \\multicolumn{1}{c}{(19)} & \\multicolumn{1}{c}{(20)} & \\multicolumn{1}{c}{(21)} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule outcome & \\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} \\\\ \n")
add.n02 <- paste ("$\\sum Y = 1$ & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[9],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[10],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[11],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[12],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[13],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[14],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[15],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[16],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[17],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[18],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[19],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[20],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab16ns[21],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 24
addtorow$pos[[4]] <- 24
addtorow$command <- c (Header1, Header2, add.n01, add.n02)
print (xtable ( cbind (rows, tab16all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017: Conditional effects (30-day window with legislator-mandate fixed effects)"
                , label="T:resultsInt30legmFE")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lmonth3, month_id_period >= 2)$name)));round (length (unique (filter (lmonth3, month_id_period >= 2)$id))); mols30_all11d$N



#### (5.1.7) Main interaction: by Month, 30-day window, Survival models ######

## constructing a matrix with the values, which we will later format for Latex
(tab17coef <- bind_cols (
  sum_surv30_all11b[2:13,1], sum_surv30_all21b[2:13,1], sum_surv30_all31b[2:13,1], rep (NA, 12)
  , sum_surv30_all12b[2:13,1], sum_surv30_all22b[2:13,1], sum_surv30_all32b[2:13,1], rep (NA, 12)
  , sum_surv30_all13b[2:13,1], sum_surv30_all23b[2:13,1], sum_surv30_all33b[2:13,1], rep (NA, 12)
  , sum_surv30_all14b[2:13,1], sum_surv30_all24b[2:13,1], sum_surv30_all34b[2:13,1], rep (NA, 12)
  , sum_surv30_all15b[2:13,1], sum_surv30_all25b[2:13,1], sum_surv30_all35b[2:13,1], rep (NA, 12)
  , sum_surv30_all16b[2:13,1], sum_surv30_all26b[2:13,1], sum_surv30_all36b[2:13,1], rep (NA, 12)
  , sum_surv30_all17b[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_surv30_all27b[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_surv30_all37b[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1]))
(tab17se <- bind_cols (
  sum_surv30_all11b[1:12,2], sum_surv30_all21b[1:12,2], sum_surv30_all31b[1:12,2], rep (NA, 12)
  , sum_surv30_all12b[1:12,2], sum_surv30_all22b[1:12,2], sum_surv30_all32b[1:12,2], rep (NA, 12)
  , sum_surv30_all13b[1:12,2], sum_surv30_all23b[1:12,2], sum_surv30_all33b[1:12,2], rep (NA, 12)
  , sum_surv30_all14b[1:12,2], sum_surv30_all24b[1:12,2], sum_surv30_all34b[1:12,2], rep (NA, 12)
  , sum_surv30_all15b[1:12,2], sum_surv30_all25b[1:12,2], sum_surv30_all35b[1:12,2], rep (NA, 12)
  , sum_surv30_all16b[1:12,2], sum_surv30_all26b[1:12,2], sum_surv30_all36b[1:12,2], rep (NA, 12)
  , sum_surv30_all17b[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_surv30_all27b[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_surv30_all37b[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2]))

## creating the table
(tab17all <- parSE (bind_rows (tab17coef, tab17se)[sort (rep (1:12, 2)) + rep (c (0, 12), 2),], n.digit=digits))

## formatting for Latex
rows <- c ("national (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "[1.5ex] provincial (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , "")

(tab17ns <- c (sum (mols30_all11b$response/100), sum (mols30_all21b$response/100), sum (mols30_all31b$response/100), sum (mols30_all12b$response/100), sum (mols30_all22b$response/100), sum (mols30_all32b$response/100), sum (mols30_all13b$response/100), sum (mols30_all23b$response/100), sum (mols30_all33b$response/100), sum (mols30_all14b$response/100), sum (mols30_all24b$response/100), sum (mols30_all34b$response/100), sum (mols30_all15b$response/100), sum (mols30_all25b$response/100), sum (mols30_all35b$response/100), sum (mols30_all16b$response/100), sum (mols30_all26b$response/100), sum (mols30_all36b$response/100), sum (mols30_all17b$response/100), sum (mols30_all27b$response/100), sum (mols30_all37b$response/100) ))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{position in list}} & & \\multicolumn{3}{c}{\\textbf{elect. vulnerable}} & & \\multicolumn{3}{c}{\\textbf{previous experience}} & & \\multicolumn{3}{c}{\\textbf{committee chair}} & & \\multicolumn{3}{c}{\\textbf{elected in midterm}} & & \\multicolumn{3}{c}{\\textbf{female}} & & \\multicolumn{3}{c}{\\textbf{party (\\textsc{ucr}/\\textsc{pj})}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \\cmidrule{14-16} \\cmidrule{18-20} \\cmidrule{22-24} \\cmidrule{26-28} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} & & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} & & \\multicolumn{1}{c}{(13)} & \\multicolumn{1}{c}{(14)} & \\multicolumn{1}{c}{(15)} & & \\multicolumn{1}{c}{(16)} & \\multicolumn{1}{c}{(17)} & \\multicolumn{1}{c}{(18)} & & \\multicolumn{1}{c}{(19)} & \\multicolumn{1}{c}{(20)} & \\multicolumn{1}{c}{(21)} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule outcome & \\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} \\\\ \n")
add.n02 <- paste ("$\\sum Y = 1$ & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[9],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[10],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[11],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[12],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[13],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[14],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[15],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[16],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[17],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[18],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[19],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[20],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tab17ns[21],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 24
addtorow$pos[[4]] <- 24
addtorow$command <- c (Header1, Header2, add.n01, add.n02)
print (xtable ( cbind (rows, tab17all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017: Conditional effects (30-day window, survival models)"
                , label="T:resultsInt30surv")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lmonth3, month_id_period >= 2)$name)));round (length (unique (filter (lmonth3, month_id_period >= 2)$id))); mols30_all11b$N




###### (5.2) Robustness (I): Daily data --> in a separate file ######




###### (5.3) Robustness (II): DiD ######

##### (5.3.1) Imai, Kim and Wang (2020): Matching + DiD ####

### (5.3.1.1) constructing the table with values ####
ikw_estims_full <- bind_cols (
  
  ## ID's
  c (rep ('basic', 108), rep ('district', 108))
  , rep (c (rep ('alternation at the\nnational level', 54), rep ('alternation at the\nprovincial level', 54)), 2)
  , rep (c (rep ('same party,\nsame person', 18), rep ('same party,\ndifferent person', 18), rep ('different party,\ndifferent person', 18)), 4)
  , rep (c (rep ('resignation (0/100)', 6), rep ('resignation & election (0/100)', 6), rep ('resignation & appointment (0/100)', 6)), 12)
  , rep (0:5, 36)
  
  ## basic matched set
  , bind_rows (
    
    # national, same party, same person
    c (coef (sum_ikw_nat_spsp110)[1,1:2], length (sum_ikw_nat_spsp110$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp111)[1,1:2], length (sum_ikw_nat_spsp111$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp112)[1,1:2], length (sum_ikw_nat_spsp112$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp113)[1,1:2], length (sum_ikw_nat_spsp113$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp114)[1,1:2], length (sum_ikw_nat_spsp114$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp115)[1,1:2], length (sum_ikw_nat_spsp115$fe$mset01_month))
    
    , c (coef (sum_ikw_nat_spsp120)[1,1:2], length (sum_ikw_nat_spsp120$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp121)[1,1:2], length (sum_ikw_nat_spsp121$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp122)[1,1:2], length (sum_ikw_nat_spsp122$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp123)[1,1:2], length (sum_ikw_nat_spsp123$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp124)[1,1:2], length (sum_ikw_nat_spsp124$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp125)[1,1:2], length (sum_ikw_nat_spsp125$fe$mset01_month))
    
    , c (coef (sum_ikw_nat_spsp130)[1,1:2], length (sum_ikw_nat_spsp130$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp131)[1,1:2], length (sum_ikw_nat_spsp131$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp132)[1,1:2], length (sum_ikw_nat_spsp132$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp133)[1,1:2], length (sum_ikw_nat_spsp133$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp134)[1,1:2], length (sum_ikw_nat_spsp134$fe$mset01_month))
    , c (coef (sum_ikw_nat_spsp135)[1,1:2], length (sum_ikw_nat_spsp135$fe$mset01_month))
    
    # national, same party, different person
    , c (coef (sum_ikw_nat_spdp110)[1,1:2], length (sum_ikw_nat_spdp110$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp111)[1,1:2], length (sum_ikw_nat_spdp111$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp112)[1,1:2], length (sum_ikw_nat_spdp112$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp113)[1,1:2], length (sum_ikw_nat_spdp113$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp114)[1,1:2], length (sum_ikw_nat_spdp114$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp115)[1,1:2], length (sum_ikw_nat_spdp115$fe$mset01_month))
    
    , c (coef (sum_ikw_nat_spdp120)[1,1:2], length (sum_ikw_nat_spdp120$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp121)[1,1:2], length (sum_ikw_nat_spdp121$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp122)[1,1:2], length (sum_ikw_nat_spdp122$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp123)[1,1:2], length (sum_ikw_nat_spdp123$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp124)[1,1:2], length (sum_ikw_nat_spdp124$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp125)[1,1:2], length (sum_ikw_nat_spdp125$fe$mset01_month))
    
    , c (coef (sum_ikw_nat_spdp130)[1,1:2], length (sum_ikw_nat_spdp130$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp131)[1,1:2], length (sum_ikw_nat_spdp131$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp132)[1,1:2], length (sum_ikw_nat_spdp132$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp133)[1,1:2], length (sum_ikw_nat_spdp133$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp134)[1,1:2], length (sum_ikw_nat_spdp134$fe$mset01_month))
    , c (coef (sum_ikw_nat_spdp135)[1,1:2], length (sum_ikw_nat_spdp135$fe$mset01_month))
    
    # national, different party, different person
    , c (coef (sum_ikw_nat_dpdp110)[1,1:2], length (sum_ikw_nat_dpdp110$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp111)[1,1:2], length (sum_ikw_nat_dpdp111$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp112)[1,1:2], length (sum_ikw_nat_dpdp112$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp113)[1,1:2], length (sum_ikw_nat_dpdp113$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp114)[1,1:2], length (sum_ikw_nat_dpdp114$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp115)[1,1:2], length (sum_ikw_nat_dpdp115$fe$mset01_month))
    
    , c (coef (sum_ikw_nat_dpdp120)[1,1:2], length (sum_ikw_nat_dpdp120$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp121)[1,1:2], length (sum_ikw_nat_dpdp121$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp122)[1,1:2], length (sum_ikw_nat_dpdp122$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp123)[1,1:2], length (sum_ikw_nat_dpdp123$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp124)[1,1:2], length (sum_ikw_nat_dpdp124$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp125)[1,1:2], length (sum_ikw_nat_dpdp125$fe$mset01_month))
    
    , c (coef (sum_ikw_nat_dpdp130)[1,1:2], length (sum_ikw_nat_dpdp130$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp131)[1,1:2], length (sum_ikw_nat_dpdp131$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp132)[1,1:2], length (sum_ikw_nat_dpdp132$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp133)[1,1:2], length (sum_ikw_nat_dpdp133$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp134)[1,1:2], length (sum_ikw_nat_dpdp134$fe$mset01_month))
    , c (coef (sum_ikw_nat_dpdp135)[1,1:2], length (sum_ikw_nat_dpdp135$fe$mset01_month))
    
    # provincial, same party, same person
    , c (coef (sum_ikw_prov_spsp110)[1,1:2], length (sum_ikw_prov_spsp110$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp111)[1,1:2], length (sum_ikw_prov_spsp111$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp112)[1,1:2], length (sum_ikw_prov_spsp112$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp113)[1,1:2], length (sum_ikw_prov_spsp113$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp114)[1,1:2], length (sum_ikw_prov_spsp114$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp115)[1,1:2], length (sum_ikw_prov_spsp115$fe$mset01_month))
    
    , c (coef (sum_ikw_prov_spsp120)[1,1:2], length (sum_ikw_prov_spsp120$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp121)[1,1:2], length (sum_ikw_prov_spsp121$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp122)[1,1:2], length (sum_ikw_prov_spsp122$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp123)[1,1:2], length (sum_ikw_prov_spsp123$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp124)[1,1:2], length (sum_ikw_prov_spsp124$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp125)[1,1:2], length (sum_ikw_prov_spsp125$fe$mset01_month))
    
    , c (coef (sum_ikw_prov_spsp130)[1,1:2], length (sum_ikw_prov_spsp130$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp131)[1,1:2], length (sum_ikw_prov_spsp131$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp132)[1,1:2], length (sum_ikw_prov_spsp132$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp133)[1,1:2], length (sum_ikw_prov_spsp133$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp134)[1,1:2], length (sum_ikw_prov_spsp134$fe$mset01_month))
    , c (coef (sum_ikw_prov_spsp135)[1,1:2], length (sum_ikw_prov_spsp135$fe$mset01_month))
    
    # provincial, same party, different person
    , c (coef (sum_ikw_prov_spdp110)[1,1:2], length (sum_ikw_prov_spdp110$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp111)[1,1:2], length (sum_ikw_prov_spdp111$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp112)[1,1:2], length (sum_ikw_prov_spdp112$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp113)[1,1:2], length (sum_ikw_prov_spdp113$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp114)[1,1:2], length (sum_ikw_prov_spdp114$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp115)[1,1:2], length (sum_ikw_prov_spdp115$fe$mset01_month))
    
    , c (coef (sum_ikw_prov_spdp120)[1,1:2], length (sum_ikw_prov_spdp120$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp121)[1,1:2], length (sum_ikw_prov_spdp121$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp122)[1,1:2], length (sum_ikw_prov_spdp122$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp123)[1,1:2], length (sum_ikw_prov_spdp123$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp124)[1,1:2], length (sum_ikw_prov_spdp124$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp125)[1,1:2], length (sum_ikw_prov_spdp125$fe$mset01_month))
    
    , c (coef (sum_ikw_prov_spdp130)[1,1:2], length (sum_ikw_prov_spdp130$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp131)[1,1:2], length (sum_ikw_prov_spdp131$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp132)[1,1:2], length (sum_ikw_prov_spdp132$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp133)[1,1:2], length (sum_ikw_prov_spdp133$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp134)[1,1:2], length (sum_ikw_prov_spdp134$fe$mset01_month))
    , c (coef (sum_ikw_prov_spdp135)[1,1:2], length (sum_ikw_prov_spdp135$fe$mset01_month))
    
    # provincial, different party, different person
    , c (coef (sum_ikw_prov_dpdp110)[1,1:2], length (sum_ikw_prov_dpdp110$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp111)[1,1:2], length (sum_ikw_prov_dpdp111$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp112)[1,1:2], length (sum_ikw_prov_dpdp112$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp113)[1,1:2], length (sum_ikw_prov_dpdp113$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp114)[1,1:2], length (sum_ikw_prov_dpdp114$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp115)[1,1:2], length (sum_ikw_prov_dpdp115$fe$mset01_month))
    
    , c (coef (sum_ikw_prov_dpdp120)[1,1:2], length (sum_ikw_prov_dpdp120$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp121)[1,1:2], length (sum_ikw_prov_dpdp121$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp122)[1,1:2], length (sum_ikw_prov_dpdp122$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp123)[1,1:2], length (sum_ikw_prov_dpdp123$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp124)[1,1:2], length (sum_ikw_prov_dpdp124$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp125)[1,1:2], length (sum_ikw_prov_dpdp125$fe$mset01_month))
    
    , c (coef (sum_ikw_prov_dpdp130)[1,1:2], length (sum_ikw_prov_dpdp130$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp131)[1,1:2], length (sum_ikw_prov_dpdp131$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp132)[1,1:2], length (sum_ikw_prov_dpdp132$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp133)[1,1:2], length (sum_ikw_prov_dpdp133$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp134)[1,1:2], length (sum_ikw_prov_dpdp134$fe$mset01_month))
    , c (coef (sum_ikw_prov_dpdp135)[1,1:2], length (sum_ikw_prov_dpdp135$fe$mset01_month))
    
    ## matched set by district
    
    # national, same party, same person
    , c (coef (sum_ikw_nat_spsp210)[1,1:2], length (sum_ikw_nat_spsp210$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp211)[1,1:2], length (sum_ikw_nat_spsp211$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp212)[1,1:2], length (sum_ikw_nat_spsp212$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp213)[1,1:2], length (sum_ikw_nat_spsp213$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp214)[1,1:2], length (sum_ikw_nat_spsp214$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp215)[1,1:2], length (sum_ikw_nat_spsp215$fe$mset02_month))
    
    , c (coef (sum_ikw_nat_spsp220)[1,1:2], length (sum_ikw_nat_spsp220$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp221)[1,1:2], length (sum_ikw_nat_spsp221$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp222)[1,1:2], length (sum_ikw_nat_spsp222$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp223)[1,1:2], length (sum_ikw_nat_spsp223$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp224)[1,1:2], length (sum_ikw_nat_spsp224$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp225)[1,1:2], length (sum_ikw_nat_spsp225$fe$mset02_month))
    
    , c (coef (sum_ikw_nat_spsp230)[1,1:2], length (sum_ikw_nat_spsp230$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp231)[1,1:2], length (sum_ikw_nat_spsp231$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp232)[1,1:2], length (sum_ikw_nat_spsp232$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp233)[1,1:2], length (sum_ikw_nat_spsp233$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp234)[1,1:2], length (sum_ikw_nat_spsp234$fe$mset02_month))
    , c (coef (sum_ikw_nat_spsp235)[1,1:2], length (sum_ikw_nat_spsp235$fe$mset02_month))
    
    # national, same party, different person
    , c (coef (sum_ikw_nat_spdp210)[1,1:2], length (sum_ikw_nat_spdp210$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp211)[1,1:2], length (sum_ikw_nat_spdp211$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp212)[1,1:2], length (sum_ikw_nat_spdp212$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp213)[1,1:2], length (sum_ikw_nat_spdp213$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp214)[1,1:2], length (sum_ikw_nat_spdp214$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp215)[1,1:2], length (sum_ikw_nat_spdp215$fe$mset02_month))
    
    , c (coef (sum_ikw_nat_spdp220)[1,1:2], length (sum_ikw_nat_spdp220$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp221)[1,1:2], length (sum_ikw_nat_spdp221$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp222)[1,1:2], length (sum_ikw_nat_spdp222$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp223)[1,1:2], length (sum_ikw_nat_spdp223$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp224)[1,1:2], length (sum_ikw_nat_spdp224$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp225)[1,1:2], length (sum_ikw_nat_spdp225$fe$mset02_month))
    
    , c (coef (sum_ikw_nat_spdp230)[1,1:2], length (sum_ikw_nat_spdp230$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp231)[1,1:2], length (sum_ikw_nat_spdp231$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp232)[1,1:2], length (sum_ikw_nat_spdp232$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp233)[1,1:2], length (sum_ikw_nat_spdp233$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp234)[1,1:2], length (sum_ikw_nat_spdp234$fe$mset02_month))
    , c (coef (sum_ikw_nat_spdp235)[1,1:2], length (sum_ikw_nat_spdp235$fe$mset02_month))
    
    # national, different party, different person
    , c (coef (sum_ikw_nat_dpdp210)[1,1:2], length (sum_ikw_nat_dpdp210$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp211)[1,1:2], length (sum_ikw_nat_dpdp211$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp212)[1,1:2], length (sum_ikw_nat_dpdp212$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp213)[1,1:2], length (sum_ikw_nat_dpdp213$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp214)[1,1:2], length (sum_ikw_nat_dpdp214$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp215)[1,1:2], length (sum_ikw_nat_dpdp215$fe$mset02_month))
    
    , c (coef (sum_ikw_nat_dpdp220)[1,1:2], length (sum_ikw_nat_dpdp220$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp221)[1,1:2], length (sum_ikw_nat_dpdp221$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp222)[1,1:2], length (sum_ikw_nat_dpdp222$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp223)[1,1:2], length (sum_ikw_nat_dpdp223$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp224)[1,1:2], length (sum_ikw_nat_dpdp224$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp225)[1,1:2], length (sum_ikw_nat_dpdp225$fe$mset02_month))
    
    , c (coef (sum_ikw_nat_dpdp230)[1,1:2], length (sum_ikw_nat_dpdp230$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp231)[1,1:2], length (sum_ikw_nat_dpdp231$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp232)[1,1:2], length (sum_ikw_nat_dpdp232$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp233)[1,1:2], length (sum_ikw_nat_dpdp233$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp234)[1,1:2], length (sum_ikw_nat_dpdp234$fe$mset02_month))
    , c (coef (sum_ikw_nat_dpdp235)[1,1:2], length (sum_ikw_nat_dpdp235$fe$mset02_month))
    
    # provincial, same party, same person
    , c (coef (sum_ikw_prov_spsp210)[1,1:2], length (sum_ikw_prov_spsp210$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp211)[1,1:2], length (sum_ikw_prov_spsp211$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp212)[1,1:2], length (sum_ikw_prov_spsp212$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp213)[1,1:2], length (sum_ikw_prov_spsp213$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp214)[1,1:2], length (sum_ikw_prov_spsp214$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp215)[1,1:2], length (sum_ikw_prov_spsp215$fe$mset02_month))
    
    , c (coef (sum_ikw_prov_spsp220)[1,1:2], length (sum_ikw_prov_spsp220$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp221)[1,1:2], length (sum_ikw_prov_spsp221$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp222)[1,1:2], length (sum_ikw_prov_spsp222$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp223)[1,1:2], length (sum_ikw_prov_spsp223$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp224)[1,1:2], length (sum_ikw_prov_spsp224$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp225)[1,1:2], length (sum_ikw_prov_spsp225$fe$mset02_month))
    
    , c (coef (sum_ikw_prov_spsp230)[1,1:2], length (sum_ikw_prov_spsp230$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp231)[1,1:2], length (sum_ikw_prov_spsp231$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp232)[1,1:2], length (sum_ikw_prov_spsp232$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp233)[1,1:2], length (sum_ikw_prov_spsp233$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp234)[1,1:2], length (sum_ikw_prov_spsp234$fe$mset02_month))
    , c (coef (sum_ikw_prov_spsp235)[1,1:2], length (sum_ikw_prov_spsp235$fe$mset02_month))
    
    # provincial, same party, different person
    , c (coef (sum_ikw_prov_spdp210)[1,1:2], length (sum_ikw_prov_spdp210$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp211)[1,1:2], length (sum_ikw_prov_spdp211$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp212)[1,1:2], length (sum_ikw_prov_spdp212$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp213)[1,1:2], length (sum_ikw_prov_spdp213$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp214)[1,1:2], length (sum_ikw_prov_spdp214$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp215)[1,1:2], length (sum_ikw_prov_spdp215$fe$mset02_month))
    
    , c (coef (sum_ikw_prov_spdp220)[1,1:2], length (sum_ikw_prov_spdp220$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp221)[1,1:2], length (sum_ikw_prov_spdp221$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp222)[1,1:2], length (sum_ikw_prov_spdp222$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp223)[1,1:2], length (sum_ikw_prov_spdp223$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp224)[1,1:2], length (sum_ikw_prov_spdp224$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp225)[1,1:2], length (sum_ikw_prov_spdp225$fe$mset02_month))
    
    , c (coef (sum_ikw_prov_spdp230)[1,1:2], length (sum_ikw_prov_spdp230$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp231)[1,1:2], length (sum_ikw_prov_spdp231$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp232)[1,1:2], length (sum_ikw_prov_spdp232$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp233)[1,1:2], length (sum_ikw_prov_spdp233$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp234)[1,1:2], length (sum_ikw_prov_spdp234$fe$mset02_month))
    , c (coef (sum_ikw_prov_spdp235)[1,1:2], length (sum_ikw_prov_spdp235$fe$mset02_month))
    
    # provincial, different party, different person
    , c (coef (sum_ikw_prov_dpdp210)[1,1:2], length (sum_ikw_prov_dpdp210$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp211)[1,1:2], length (sum_ikw_prov_dpdp211$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp212)[1,1:2], length (sum_ikw_prov_dpdp212$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp213)[1,1:2], length (sum_ikw_prov_dpdp213$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp214)[1,1:2], length (sum_ikw_prov_dpdp214$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp215)[1,1:2], length (sum_ikw_prov_dpdp215$fe$mset02_month))
    
    , c (coef (sum_ikw_prov_dpdp220)[1,1:2], length (sum_ikw_prov_dpdp220$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp221)[1,1:2], length (sum_ikw_prov_dpdp221$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp222)[1,1:2], length (sum_ikw_prov_dpdp222$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp223)[1,1:2], length (sum_ikw_prov_dpdp223$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp224)[1,1:2], length (sum_ikw_prov_dpdp224$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp225)[1,1:2], length (sum_ikw_prov_dpdp225$fe$mset02_month))
    
    , c (coef (sum_ikw_prov_dpdp230)[1,1:2], length (sum_ikw_prov_dpdp230$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp231)[1,1:2], length (sum_ikw_prov_dpdp231$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp232)[1,1:2], length (sum_ikw_prov_dpdp232$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp233)[1,1:2], length (sum_ikw_prov_dpdp233$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp234)[1,1:2], length (sum_ikw_prov_dpdp234$fe$mset02_month))
    , c (coef (sum_ikw_prov_dpdp235)[1,1:2], length (sum_ikw_prov_dpdp235$fe$mset02_month))
    
  )) %>% setNames (., c ('mset', 'level', 'treat', 'outcome', 'period', 'estim', 'se', 't_df')
  ) %>% mutate (
    mset = factor (mset, levels=c ('basic', 'district'))
    , level = factor (level, levels=c ('alternation at the\nnational level', 'alternation at the\nprovincial level'))
    , treat = factor (treat, levels=c ('same party,\nsame person', 'same party,\ndifferent person', 'different party,\ndifferent person'))
    , outcome = factor (outcome, levels=c ('resignation (0/100)', 'resignation & election (0/100)', 'resignation & appointment (0/100)'))
    , period = ifelse (outcome=='resignation & election (0/100)', period-0.2, ifelse (
      outcome=='resignation & appointment (0/100)', period+0.2, period))
    , crit_val = qt (.975, df=t_df)
    , low95 = estim - se*crit_val
    , high95 = estim + se*crit_val)
summary (ikw_estims_full)



### (5.3.1.2) drawing the plots ####

## basic matched set
(pIKW_basic <- ggplot (filter (ikw_estims_full, mset=='basic' & outcome=='resignation (0/100)')
                       , aes (x=period, y=estim))
 + geom_hline (yintercept=0, linetype=3)
 + geom_vline (xintercept=0, linetype=3)
 
 # overall effect on resignation
 + geom_ribbon (aes (ymin=low95, ymax=high95), fill='gray85')
 + geom_line (color='gray35')
 
 # disaggregating the effect by type of posterior office
 + geom_point (data=filter (ikw_estims_full, mset=='basic' & outcome!='resignation (0/100)')
               , aes (x=period, y=estim, color=outcome), size=0.95)
 + geom_errorbar (data=filter (ikw_estims_full, mset=='basic' & outcome!='resignation (0/100)')
                  , aes (ymin=low95, ymax=high95, color=outcome), width=0.1)
 
 # other graphical parameters
 + facet_grid (treat ~ level)
 + scale_y_continuous (breaks=c (-10, -5, 0, 5, 10), labels=c (-10, -5, 0, 5, 10))
 + coord_cartesian (ylim=c (-12.5, 12.5))
 + scale_x_continuous (breaks=c (0:5), labels=c (0:5))
 + scale_color_manual (values=movie)
 + ylab ('estimated effect')
 + xlab ('months relative to treatment\n(0 = month in which treatment began)')
 + theme_classic (base_size=cex_base)
 + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## matching by district
(pIKW_district <- ggplot (filter (ikw_estims_full, mset=='district' & outcome=='resignation (0/100)')
                       , aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (color='gray35')
  
  # disaggregating the effect by type of posterior office
  + geom_point (data=filter (ikw_estims_full, mset=='district' & outcome!='resignation (0/100)')
                , aes (x=period, y=estim, color=outcome), size=0.95)
  + geom_errorbar (data=filter (ikw_estims_full, mset=='district' & outcome!='resignation (0/100)')
                   , aes (ymin=low95, ymax=high95, color=outcome), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-10, -5, 0, 5, 10), labels=c (-10, -5, 0, 5, 10))
  + coord_cartesian (ylim=c (-12.5, 12.5))
  + scale_x_continuous (breaks=c (0:5), labels=c (0:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )




##### (5.3.2) Callaway and Sant'Anna (2021) DiD estimator ####

### (5.3.2.1) results by group ####

# getting the values of interest
csa_estims_group <- bind_rows (
  
  ## national alternation
  
  # same party, same person
  bind_cols ('national', 'spsp', 'resignation'
             , c (NA, csa_nat_spsp11_gr$egt)
             , c (csa_nat_spsp11_gr$overall.att, csa_nat_spsp11_gr$att.egt)
             , c (csa_nat_spsp11_gr$overall.se, csa_nat_spsp11_gr$se.egt)
             , csa_nat_spsp11_gr$crit.val.egt)
  , bind_cols ('national', 'spsp', 'election'
               , c (NA, csa_nat_spsp12_gr$egt)
               , c (csa_nat_spsp12_gr$overall.att, csa_nat_spsp12_gr$att.egt)
               , c (csa_nat_spsp12_gr$overall.se, csa_nat_spsp12_gr$se.egt)
               , csa_nat_spsp12_gr$crit.val.egt)
  , bind_cols ('national', 'spsp', 'appointment'
               , c (NA, csa_nat_spsp13_gr$egt)
               , c (csa_nat_spsp13_gr$overall.att, csa_nat_spsp13_gr$att.egt)
               , c (csa_nat_spsp13_gr$overall.se, csa_nat_spsp13_gr$se.egt)
               , csa_nat_spsp13_gr$crit.val.egt)
  
  # same party, different person
  , bind_cols ('national', 'spdp', 'resignation'
               , c (NA, csa_nat_spdp11_gr$egt)
               , c (csa_nat_spdp11_gr$overall.att, csa_nat_spdp11_gr$att.egt)
               , c (csa_nat_spdp11_gr$overall.se, csa_nat_spdp11_gr$se.egt)
               , csa_nat_spdp11_gr$crit.val.egt)
  , bind_cols ('national', 'spdp', 'election'
               , c (NA, csa_nat_spdp12_gr$egt)
               , c (csa_nat_spdp12_gr$overall.att, csa_nat_spdp12_gr$att.egt)
               , c (csa_nat_spdp12_gr$overall.se, csa_nat_spdp12_gr$se.egt)
               , csa_nat_spdp12_gr$crit.val.egt)
  , bind_cols ('national', 'spdp', 'appointment'
               , c (NA, csa_nat_spdp13_gr$egt)
               , c (csa_nat_spdp13_gr$overall.att, csa_nat_spdp13_gr$att.egt)
               , c (csa_nat_spdp13_gr$overall.se, csa_nat_spdp13_gr$se.egt)
               , csa_nat_spdp13_gr$crit.val.egt)
  
  # different party, different person
  , bind_cols ('national', 'dpdp', 'resignation'
               , c (NA, csa_nat_dpdp11_gr$egt)
               , c (csa_nat_dpdp11_gr$overall.att, csa_nat_dpdp11_gr$att.egt)
               , c (csa_nat_dpdp11_gr$overall.se, csa_nat_dpdp11_gr$se.egt)
               , csa_nat_dpdp11_gr$crit.val.egt)
  , bind_cols ('national', 'dpdp', 'election'
               , c (NA, csa_nat_dpdp12_gr$egt)
               , c (csa_nat_dpdp12_gr$overall.att, csa_nat_dpdp12_gr$att.egt)
               , c (csa_nat_dpdp12_gr$overall.se, csa_nat_dpdp12_gr$se.egt)
               , csa_nat_dpdp12_gr$crit.val.egt)
  , bind_cols ('national', 'dpdp', 'appointment'
               , c (NA, csa_nat_dpdp13_gr$egt)
               , c (csa_nat_dpdp13_gr$overall.att, csa_nat_dpdp13_gr$att.egt)
               , c (csa_nat_dpdp13_gr$overall.se, csa_nat_dpdp13_gr$se.egt)
               , csa_nat_dpdp13_gr$crit.val.egt)
  
  
  ## provincial alternation
  
  # same party, same person
  , bind_cols ('provincial', 'spsp', 'resignation'
               , c (NA, csa_prov_spsp11_gr$egt)
               , c (csa_prov_spsp11_gr$overall.att, csa_prov_spsp11_gr$att.egt)
               , c (csa_prov_spsp11_gr$overall.se, csa_prov_spsp11_gr$se.egt)
               , csa_prov_spsp11_gr$crit.val.egt)
  , bind_cols ('provincial', 'spsp', 'election'
               , c (NA, csa_prov_spsp12_gr$egt)
               , c (csa_prov_spsp12_gr$overall.att, csa_prov_spsp12_gr$att.egt)
               , c (csa_prov_spsp12_gr$overall.se, csa_prov_spsp12_gr$se.egt)
               , csa_prov_spsp12_gr$crit.val.egt)
  , bind_cols ('provincial', 'spsp', 'appointment'
               , c (NA, csa_prov_spsp13_gr$egt)
               , c (csa_prov_spsp13_gr$overall.att, csa_prov_spsp13_gr$att.egt)
               , c (csa_prov_spsp13_gr$overall.se, csa_prov_spsp13_gr$se.egt)
               , csa_prov_spsp13_gr$crit.val.egt)
  
  # same party, different person
  , bind_cols ('provincial', 'spdp', 'resignation'
               , c (NA, csa_prov_spdp11_gr$egt)
               , c (csa_prov_spdp11_gr$overall.att, csa_prov_spdp11_gr$att.egt)
               , c (csa_prov_spdp11_gr$overall.se, csa_prov_spdp11_gr$se.egt)
               , csa_prov_spdp11_gr$crit.val.egt)
  , bind_cols ('provincial', 'spdp', 'election'
               , c (NA, csa_prov_spdp12_gr$egt)
               , c (csa_prov_spdp12_gr$overall.att, csa_prov_spdp12_gr$att.egt)
               , c (csa_prov_spdp12_gr$overall.se, csa_prov_spdp12_gr$se.egt)
               , csa_prov_spdp12_gr$crit.val.egt)
  , bind_cols ('provincial', 'spdp', 'appointment'
               , c (NA, csa_prov_spdp13_gr$egt)
               , c (csa_prov_spdp13_gr$overall.att, csa_prov_spdp13_gr$att.egt)
               , c (csa_prov_spdp13_gr$overall.se, csa_prov_spdp13_gr$se.egt)
               , csa_prov_spdp13_gr$crit.val.egt)
  
  # different party, different person
  , bind_cols ('provincial', 'dpdp', 'resignation'
               , c (NA, csa_prov_dpdp11_gr$egt)
               , c (csa_prov_dpdp11_gr$overall.att, csa_prov_dpdp11_gr$att.egt)
               , c (csa_prov_dpdp11_gr$overall.se, csa_prov_dpdp11_gr$se.egt)
               , csa_prov_dpdp11_gr$crit.val.egt)
  , bind_cols ('provincial', 'dpdp', 'election'
               , c (NA, csa_prov_dpdp12_gr$egt)
               , c (csa_prov_dpdp12_gr$overall.att, csa_prov_dpdp12_gr$att.egt)
               , c (csa_prov_dpdp12_gr$overall.se, csa_prov_dpdp12_gr$se.egt)
               , csa_prov_dpdp12_gr$crit.val.egt)
  , bind_cols ('provincial', 'dpdp', 'appointment'
               , c (NA, csa_prov_dpdp13_gr$egt)
               , c (csa_prov_dpdp13_gr$overall.att, csa_prov_dpdp13_gr$att.egt)
               , c (csa_prov_dpdp13_gr$overall.se, csa_prov_dpdp13_gr$se.egt)
               , csa_prov_dpdp13_gr$crit.val.egt)
) %>% setNames (., c ('level', 'treat', 'outcome', 'group', 'estim', 'se', 'crit_val')) %>% mutate (
  level = factor (level, levels=c ('national', 'provincial'))
  , treat = factor (treat, levels=c ('spsp', 'spdp', 'dpdp'))
  , outcome = factor (outcome, levels=c ('resignation', 'election', 'appointment'))
  , group = ifelse (is.na (group), 'overall effect', as.character (group))
  , group = factor (group)
  , estim_text = sprintf ("%.2f", round (estim, 2))
  , crit_val = ifelse (group == 'overall', 1.96, crit_val)
  , low95 = estim - se*crit_val
  , high95 = estim + se*crit_val
  , ci = str_c ('[', sprintf ("%.2f", round (low95, 2)), ':', sprintf ("%.2f", round (high95, 2)), ']') )
summary (csa_estims_group)

# building the table with values
tabCSAgroup <- bind_rows (
  bind_cols (select (filter (csa_estims_group, level=='national' & treat=='spsp' & outcome=='resignation'), group, estim_text, ci)
             , select (filter (csa_estims_group, level=='national' & treat=='spsp' & outcome=='election'), estim_text, ci)
             , select (filter (csa_estims_group, level=='national' & treat=='spsp' & outcome=='appointment'), estim_text, ci) )
  , bind_cols (select (filter (csa_estims_group, level=='national' & treat=='spdp' & outcome=='resignation'), group, estim_text, ci)
               , select (filter (csa_estims_group, level=='national' & treat=='spdp' & outcome=='election'), estim_text, ci)
               , select (filter (csa_estims_group, level=='national' & treat=='spdp' & outcome=='appointment'), estim_text, ci) )
  , bind_cols (select (filter (csa_estims_group, level=='national' & treat=='dpdp' & outcome=='resignation'), group, estim_text, ci)
               , select (filter (csa_estims_group, level=='national' & treat=='dpdp' & outcome=='election'), estim_text, ci)
               , select (filter (csa_estims_group, level=='national' & treat=='dpdp' & outcome=='appointment'), estim_text, ci) )
  
  , bind_cols (select (filter (csa_estims_group, level=='provincial' & treat=='spsp' & outcome=='resignation'), group, estim_text, ci)
               , select (filter (csa_estims_group, level=='provincial' & treat=='spsp' & outcome=='election'), estim_text, ci)
               , select (filter (csa_estims_group, level=='provincial' & treat=='spsp' & outcome=='appointment'), estim_text, ci) )
  , bind_cols (select (filter (csa_estims_group, level=='provincial' & treat=='spdp' & outcome=='resignation'), group, estim_text, ci)
               , select (filter (csa_estims_group, level=='provincial' & treat=='spdp' & outcome=='election'), estim_text, ci)
               , select (filter (csa_estims_group, level=='provincial' & treat=='spdp' & outcome=='appointment'), estim_text, ci) )
  , bind_cols (select (filter (csa_estims_group, level=='provincial' & treat=='dpdp' & outcome=='resignation'), group, estim_text, ci)
               , select (filter (csa_estims_group, level=='provincial' & treat=='dpdp' & outcome=='election'), estim_text, ci)
               , select (filter (csa_estims_group, level=='provincial' & treat=='dpdp' & outcome=='appointment'), estim_text, ci) ) )
tabCSAgroup

# formatting for Latex
rows <- c ("national:", "~~~~$=$ \\emph{party,} $=$ \\emph{person}", rep ("", 3)
           , "national:", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}", rep ("", 3)
           , "national:", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}", rep ("", 3)
           , "provincial:", "~~~~$=$ \\emph{party,} $=$ \\emph{person}", rep ("", 8)
           , "provincial:", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}", rep ("", 8)
           , "provincial:", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}", rep ("", 10))

Header1 <- paste ("\\toprule & & \\multicolumn{2}{c}{\\emph{resignation}} & & \\multicolumn{2}{c}{\\emph{resignation \\& election}} & & \\multicolumn{2}{c}{\\emph{resignation \\& app'tment}} \\\\ \\cmidrule{3-4} \\cmidrule{6-7} \\cmidrule{9-10} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{group} & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{95\\% \\textsc{ci}} & & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{95\\% \\textsc{ci}} & & \\multicolumn{1}{c}{estim.} & \\multicolumn{1}{c}{95\\% \\textsc{ci}} \\\\ \\midrule \n")
add.n01 <- paste ("\\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 5
addtorow$pos[[4]] <- 10
addtorow$pos[[5]] <- 15
addtorow$pos[[6]] <- 25
addtorow$pos[[7]] <- 35
addtorow$command <- c (Header1, Header2
                       , add.n01, add.n01, add.n01, add.n01, add.n01)
print (xtable (cbind (rows, as.matrix (tabCSAgroup)[,1:3], rep ('', nrow (tabCSAgroup)), as.matrix (tabCSAgroup)[,4:5], rep ('', nrow (tabCSAgroup)), as.matrix (tabCSAgroup)[,6:7])
               , align=c ('l','l','c','c','c','c','c','c','c','c','c')
               , digits=digits
               , caption="Executive alternation and legislative resignations in Argentina, 1983-2017: Difference-in-Differences estimates by group (\\citealt{callaway_santanna2021})"
               , label="T:resultsDiDgroup")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )



### (5.3.2.2) event-study plots ####

## constructing the table with values: full sample
csa_estims_full <- bind_cols (
  
  ## ID's
  c (rep ('alternation at the\nnational level', 108), rep ('alternation at the\nprovincial level', 108))
  , rep (c (rep ('same party,\nsame person', 36), rep ('same party,\ndifferent person', 36), rep ('different party,\ndifferent person', 36)), 2)
  , rep (c (rep ('resignation (0/100)', 12), rep ('resignation & election (0/100)', 12), rep ('resignation & appointment (0/100)', 12)), 6)
  
  ## estimates
  , bind_rows (
    
    ## national alternation
    
    # same party, same person
    bind_cols (c (NA, csa_nat_spsp11_es$egt)
               , c (csa_nat_spsp11_es$overall.att, csa_nat_spsp11_es$att.egt)
               , c (csa_nat_spsp11_es$overall.se, csa_nat_spsp11_es$se.egt)
               , csa_nat_spsp11_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_nat_spsp12_es$egt)
                 , c (csa_nat_spsp12_es$overall.att, csa_nat_spsp12_es$att.egt)
                 , c (csa_nat_spsp12_es$overall.se, csa_nat_spsp12_es$se.egt)
                 , csa_nat_spsp12_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_nat_spsp13_es$egt)
                 , c (csa_nat_spsp13_es$overall.att, csa_nat_spsp13_es$att.egt)
                 , c (csa_nat_spsp13_es$overall.se, csa_nat_spsp13_es$se.egt)
                 , csa_nat_spsp13_es$crit.val.egt)
    
    # same party, different person
    , bind_cols (c (NA, csa_nat_spdp11_es$egt)
                 , c (csa_nat_spdp11_es$overall.att, csa_nat_spdp11_es$att.egt)
                 , c (csa_nat_spdp11_es$overall.se, csa_nat_spdp11_es$se.egt)
                 , csa_nat_spdp11_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_nat_spdp12_es$egt)
                 , c (csa_nat_spdp12_es$overall.att, csa_nat_spdp12_es$att.egt)
                 , c (csa_nat_spdp12_es$overall.se, csa_nat_spdp12_es$se.egt)
                 , csa_nat_spdp12_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_nat_spdp13_es$egt)
                 , c (csa_nat_spdp13_es$overall.att, csa_nat_spdp13_es$att.egt)
                 , c (csa_nat_spdp13_es$overall.se, csa_nat_spdp13_es$se.egt)
                 , csa_nat_spdp13_es$crit.val.egt)
    
    # different party, different person
    , bind_cols (c (NA, csa_nat_dpdp11_es$egt)
                 , c (csa_nat_dpdp11_es$overall.att, csa_nat_dpdp11_es$att.egt)
                 , c (csa_nat_dpdp11_es$overall.se, csa_nat_dpdp11_es$se.egt)
                 , csa_nat_dpdp11_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_nat_dpdp12_es$egt)
                 , c (csa_nat_dpdp12_es$overall.att, csa_nat_dpdp12_es$att.egt)
                 , c (csa_nat_dpdp12_es$overall.se, csa_nat_dpdp12_es$se.egt)
                 , csa_nat_dpdp12_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_nat_dpdp13_es$egt)
                 , c (csa_nat_dpdp13_es$overall.att, csa_nat_dpdp13_es$att.egt)
                 , c (csa_nat_dpdp13_es$overall.se, csa_nat_dpdp13_es$se.egt)
                 , csa_nat_dpdp13_es$crit.val.egt)
    
    ## provincial alternation
    
    # same party, same person
    , bind_cols (c (NA, csa_prov_spsp11_es$egt)
                 , c (csa_prov_spsp11_es$overall.att, csa_prov_spsp11_es$att.egt)
                 , c (csa_prov_spsp11_es$overall.se, csa_prov_spsp11_es$se.egt)
                 , csa_prov_spsp11_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_prov_spsp12_es$egt)
                 , c (csa_prov_spsp12_es$overall.att, csa_prov_spsp12_es$att.egt)
                 , c (csa_prov_spsp12_es$overall.se, csa_prov_spsp12_es$se.egt)
                 , csa_prov_spsp12_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_prov_spsp13_es$egt)
                 , c (csa_prov_spsp13_es$overall.att, csa_prov_spsp13_es$att.egt)
                 , c (csa_prov_spsp13_es$overall.se, csa_prov_spsp13_es$se.egt)
                 , csa_prov_spsp13_es$crit.val.egt)
    
    # same party, different person
    , bind_cols (c (NA, csa_prov_spdp11_es$egt)
                 , c (csa_prov_spdp11_es$overall.att, csa_prov_spdp11_es$att.egt)
                 , c (csa_prov_spdp11_es$overall.se, csa_prov_spdp11_es$se.egt)
                 , csa_prov_spdp11_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_prov_spdp12_es$egt)
                 , c (csa_prov_spdp12_es$overall.att, csa_prov_spdp12_es$att.egt)
                 , c (csa_prov_spdp12_es$overall.se, csa_prov_spdp12_es$se.egt)
                 , csa_prov_spdp12_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_prov_spdp13_es$egt)
                 , c (csa_prov_spdp13_es$overall.att, csa_prov_spdp13_es$att.egt)
                 , c (csa_prov_spdp13_es$overall.se, csa_prov_spdp13_es$se.egt)
                 , csa_prov_spdp13_es$crit.val.egt)
    
    # different party, different person
    , bind_cols (c (NA, csa_prov_dpdp11_es$egt)
                 , c (csa_prov_dpdp11_es$overall.att, csa_prov_dpdp11_es$att.egt)
                 , c (csa_prov_dpdp11_es$overall.se, csa_prov_dpdp11_es$se.egt)
                 , csa_prov_dpdp11_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_prov_dpdp12_es$egt)
                 , c (csa_prov_dpdp12_es$overall.att, csa_prov_dpdp12_es$att.egt)
                 , c (csa_prov_dpdp12_es$overall.se, csa_prov_dpdp12_es$se.egt)
                 , csa_prov_dpdp12_es$crit.val.egt)
    
    , bind_cols (c (NA, csa_prov_dpdp13_es$egt)
                 , c (csa_prov_dpdp13_es$overall.att, csa_prov_dpdp13_es$att.egt)
                 , c (csa_prov_dpdp13_es$overall.se, csa_prov_dpdp13_es$se.egt)
                 , csa_prov_dpdp13_es$crit.val.egt)
  )) %>% setNames (., c ('level', 'treat', 'outcome', 'period', 'estim', 'se', 'crit_val')
) %>% mutate (
  level = factor (level, levels=c ('alternation at the\nnational level', 'alternation at the\nprovincial level'))
  , treat = factor (treat, levels=c ('same party,\nsame person', 'same party,\ndifferent person', 'different party,\ndifferent person'))
  , outcome = factor (outcome, levels=c ('resignation (0/100)', 'resignation & election (0/100)', 'resignation & appointment (0/100)'))
  , period = ifelse (outcome=='resignation & election (0/100)', period-0.125, ifelse (
    outcome=='resignation & appointment (0/100)', period+0.125, period))
  , low95 = estim - se*crit_val
  , high95 = estim + se*crit_val)
summary (csa_estims_full)



### (5.3.2.2) constructing the table with values: subsamples
csa_estims_sub <- bind_cols (
  
  ## ID's
  c (rep ('alternation at the\nnational level', 504), rep ('alternation at the\nprovincial level', 504))
  , rep (c (rep ('same party,\nsame person', 168), rep ('same party,\ndifferent person', 168), rep ('different party,\ndifferent person', 168)), 2)
  , rep (c (
    rep ('placed 1st in party list', 12)
    , rep ('placed 2nd or lower in party list', 12)
    , rep ('not vulnerable', 12)
    , rep ('vulnerable', 12)
    , rep ('previous executive\nor legislative experience', 12)
    , rep ('no previous experience', 12)
    , rep ('committee chair', 12)
    , rep ('no committee chairmanship', 12)
    , rep ('elected in midterm year', 12)
    , rep ('elected in concurrent year', 12)
    , rep ('PJ', 12)
    , rep ('UCR', 12)
    , rep ('man', 12)
    , rep ('woman', 12)), 6)

  
  ## estimates
  , bind_rows (
    
    ## national alternation
    
    # same party, same person
    bind_cols (c (NA, csa_nat_spsp31_es$egt), c (csa_nat_spsp31_es$overall.att, csa_nat_spsp31_es$att.egt), c (csa_nat_spsp31_es$overall.se, csa_nat_spsp31_es$se.egt), csa_nat_spsp31_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp32_es$egt), c (csa_nat_spsp32_es$overall.att, csa_nat_spsp32_es$att.egt), c (csa_nat_spsp32_es$overall.se, csa_nat_spsp32_es$se.egt), csa_nat_spsp32_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp41_es$egt), c (csa_nat_spsp41_es$overall.att, csa_nat_spsp41_es$att.egt), c (csa_nat_spsp41_es$overall.se, csa_nat_spsp41_es$se.egt), csa_nat_spsp41_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp42_es$egt), c (csa_nat_spsp42_es$overall.att, csa_nat_spsp42_es$att.egt), c (csa_nat_spsp42_es$overall.se, csa_nat_spsp42_es$se.egt), csa_nat_spsp42_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp51_es$egt), c (csa_nat_spsp51_es$overall.att, csa_nat_spsp51_es$att.egt), c (csa_nat_spsp51_es$overall.se, csa_nat_spsp51_es$se.egt), csa_nat_spsp51_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp52_es$egt), c (csa_nat_spsp52_es$overall.att, csa_nat_spsp52_es$att.egt), c (csa_nat_spsp52_es$overall.se, csa_nat_spsp52_es$se.egt), csa_nat_spsp52_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp61_es$egt), c (csa_nat_spsp61_es$overall.att, csa_nat_spsp61_es$att.egt), c (csa_nat_spsp61_es$overall.se, csa_nat_spsp61_es$se.egt), csa_nat_spsp61_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp62_es$egt), c (csa_nat_spsp62_es$overall.att, csa_nat_spsp62_es$att.egt), c (csa_nat_spsp62_es$overall.se, csa_nat_spsp62_es$se.egt), csa_nat_spsp62_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp71_es$egt), c (csa_nat_spsp71_es$overall.att, csa_nat_spsp71_es$att.egt), c (csa_nat_spsp71_es$overall.se, csa_nat_spsp71_es$se.egt), csa_nat_spsp71_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp72_es$egt), c (csa_nat_spsp72_es$overall.att, csa_nat_spsp72_es$att.egt), c (csa_nat_spsp72_es$overall.se, csa_nat_spsp72_es$se.egt), csa_nat_spsp72_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp81_es$egt), c (csa_nat_spsp81_es$overall.att, csa_nat_spsp81_es$att.egt), c (csa_nat_spsp81_es$overall.se, csa_nat_spsp81_es$se.egt), csa_nat_spsp81_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp82_es$egt), c (csa_nat_spsp82_es$overall.att, csa_nat_spsp82_es$att.egt), c (csa_nat_spsp82_es$overall.se, csa_nat_spsp82_es$se.egt), csa_nat_spsp82_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp91_es$egt), c (csa_nat_spsp91_es$overall.att, csa_nat_spsp91_es$att.egt), c (csa_nat_spsp91_es$overall.se, csa_nat_spsp91_es$se.egt), csa_nat_spsp91_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spsp92_es$egt), c (csa_nat_spsp92_es$overall.att, csa_nat_spsp92_es$att.egt), c (csa_nat_spsp92_es$overall.se, csa_nat_spsp92_es$se.egt), csa_nat_spsp92_es$crit.val.egt)
    
    # same party, different person
    , bind_cols (c (NA, csa_nat_spdp31_es$egt), c (csa_nat_spdp31_es$overall.att, csa_nat_spdp31_es$att.egt), c (csa_nat_spdp31_es$overall.se, csa_nat_spdp31_es$se.egt), csa_nat_spdp31_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp32_es$egt), c (csa_nat_spdp32_es$overall.att, csa_nat_spdp32_es$att.egt), c (csa_nat_spdp32_es$overall.se, csa_nat_spdp32_es$se.egt), csa_nat_spdp32_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp41_es$egt), c (csa_nat_spdp41_es$overall.att, csa_nat_spdp41_es$att.egt), c (csa_nat_spdp41_es$overall.se, csa_nat_spdp41_es$se.egt), csa_nat_spdp41_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp42_es$egt), c (csa_nat_spdp42_es$overall.att, csa_nat_spdp42_es$att.egt), c (csa_nat_spdp42_es$overall.se, csa_nat_spdp42_es$se.egt), csa_nat_spdp42_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp51_es$egt), c (csa_nat_spdp51_es$overall.att, csa_nat_spdp51_es$att.egt), c (csa_nat_spdp51_es$overall.se, csa_nat_spdp51_es$se.egt), csa_nat_spdp51_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp52_es$egt), c (csa_nat_spdp52_es$overall.att, csa_nat_spdp52_es$att.egt), c (csa_nat_spdp52_es$overall.se, csa_nat_spdp52_es$se.egt), csa_nat_spdp52_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp61_es$egt), c (csa_nat_spdp61_es$overall.att, csa_nat_spdp61_es$att.egt), c (csa_nat_spdp61_es$overall.se, csa_nat_spdp61_es$se.egt), csa_nat_spdp61_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp62_es$egt), c (csa_nat_spdp62_es$overall.att, csa_nat_spdp62_es$att.egt), c (csa_nat_spdp62_es$overall.se, csa_nat_spdp62_es$se.egt), csa_nat_spdp62_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp71_es$egt), c (csa_nat_spdp71_es$overall.att, csa_nat_spdp71_es$att.egt), c (csa_nat_spdp71_es$overall.se, csa_nat_spdp71_es$se.egt), csa_nat_spdp71_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp72_es$egt), c (csa_nat_spdp72_es$overall.att, csa_nat_spdp72_es$att.egt), c (csa_nat_spdp72_es$overall.se, csa_nat_spdp72_es$se.egt), csa_nat_spdp72_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp81_es$egt), c (csa_nat_spdp81_es$overall.att, csa_nat_spdp81_es$att.egt), c (csa_nat_spdp81_es$overall.se, csa_nat_spdp81_es$se.egt), csa_nat_spdp81_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp82_es$egt), c (csa_nat_spdp82_es$overall.att, csa_nat_spdp82_es$att.egt), c (csa_nat_spdp82_es$overall.se, csa_nat_spdp82_es$se.egt), csa_nat_spdp82_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp91_es$egt), c (csa_nat_spdp91_es$overall.att, csa_nat_spdp91_es$att.egt), c (csa_nat_spdp91_es$overall.se, csa_nat_spdp91_es$se.egt), csa_nat_spdp91_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_spdp92_es$egt), c (csa_nat_spdp92_es$overall.att, csa_nat_spdp92_es$att.egt), c (csa_nat_spdp92_es$overall.se, csa_nat_spdp92_es$se.egt), csa_nat_spdp92_es$crit.val.egt)
    
    # different party, different person
    , bind_cols (c (NA, csa_nat_dpdp31_es$egt), c (csa_nat_dpdp31_es$overall.att, csa_nat_dpdp31_es$att.egt), c (csa_nat_dpdp31_es$overall.se, csa_nat_dpdp31_es$se.egt), csa_nat_dpdp31_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp32_es$egt), c (csa_nat_dpdp32_es$overall.att, csa_nat_dpdp32_es$att.egt), c (csa_nat_dpdp32_es$overall.se, csa_nat_dpdp32_es$se.egt), csa_nat_dpdp32_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp41_es$egt), c (csa_nat_dpdp41_es$overall.att, csa_nat_dpdp41_es$att.egt), c (csa_nat_dpdp41_es$overall.se, csa_nat_dpdp41_es$se.egt), csa_nat_dpdp41_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp42_es$egt), c (csa_nat_dpdp42_es$overall.att, csa_nat_dpdp42_es$att.egt), c (csa_nat_dpdp42_es$overall.se, csa_nat_dpdp42_es$se.egt), csa_nat_dpdp42_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp51_es$egt), c (csa_nat_dpdp51_es$overall.att, csa_nat_dpdp51_es$att.egt), c (csa_nat_dpdp51_es$overall.se, csa_nat_dpdp51_es$se.egt), csa_nat_dpdp51_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp52_es$egt), c (csa_nat_dpdp52_es$overall.att, csa_nat_dpdp52_es$att.egt), c (csa_nat_dpdp52_es$overall.se, csa_nat_dpdp52_es$se.egt), csa_nat_dpdp52_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp61_es$egt), c (csa_nat_dpdp61_es$overall.att, csa_nat_dpdp61_es$att.egt), c (csa_nat_dpdp61_es$overall.se, csa_nat_dpdp61_es$se.egt), csa_nat_dpdp61_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp62_es$egt), c (csa_nat_dpdp62_es$overall.att, csa_nat_dpdp62_es$att.egt), c (csa_nat_dpdp62_es$overall.se, csa_nat_dpdp62_es$se.egt), csa_nat_dpdp62_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp71_es$egt), c (csa_nat_dpdp71_es$overall.att, csa_nat_dpdp71_es$att.egt), c (csa_nat_dpdp71_es$overall.se, csa_nat_dpdp71_es$se.egt), csa_nat_dpdp71_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp72_es$egt), c (csa_nat_dpdp72_es$overall.att, csa_nat_dpdp72_es$att.egt), c (csa_nat_dpdp72_es$overall.se, csa_nat_dpdp72_es$se.egt), csa_nat_dpdp72_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp81_es$egt), c (csa_nat_dpdp81_es$overall.att, csa_nat_dpdp81_es$att.egt), c (csa_nat_dpdp81_es$overall.se, csa_nat_dpdp81_es$se.egt), csa_nat_dpdp81_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp82_es$egt), c (csa_nat_dpdp82_es$overall.att, csa_nat_dpdp82_es$att.egt), c (csa_nat_dpdp82_es$overall.se, csa_nat_dpdp82_es$se.egt), csa_nat_dpdp82_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp91_es$egt), c (csa_nat_dpdp91_es$overall.att, csa_nat_dpdp91_es$att.egt), c (csa_nat_dpdp91_es$overall.se, csa_nat_dpdp91_es$se.egt), csa_nat_dpdp91_es$crit.val.egt)
    , bind_cols (c (NA, csa_nat_dpdp92_es$egt), c (csa_nat_dpdp92_es$overall.att, csa_nat_dpdp92_es$att.egt), c (csa_nat_dpdp92_es$overall.se, csa_nat_dpdp92_es$se.egt), csa_nat_dpdp92_es$crit.val.egt)
    
    
    ## provincial alternation
    
    # same party, same person
    , bind_cols (c (NA, csa_prov_spsp31_es$egt), c (csa_prov_spsp31_es$overall.att, csa_prov_spsp31_es$att.egt), c (csa_prov_spsp31_es$overall.se, csa_prov_spsp31_es$se.egt), csa_prov_spsp31_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp32_es$egt), c (csa_prov_spsp32_es$overall.att, csa_prov_spsp32_es$att.egt), c (csa_prov_spsp32_es$overall.se, csa_prov_spsp32_es$se.egt), csa_prov_spsp32_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp41_es$egt), c (csa_prov_spsp41_es$overall.att, csa_prov_spsp41_es$att.egt), c (csa_prov_spsp41_es$overall.se, csa_prov_spsp41_es$se.egt), csa_prov_spsp41_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp42_es$egt), c (csa_prov_spsp42_es$overall.att, csa_prov_spsp42_es$att.egt), c (csa_prov_spsp42_es$overall.se, csa_prov_spsp42_es$se.egt), csa_prov_spsp42_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp51_es$egt), c (csa_prov_spsp51_es$overall.att, csa_prov_spsp51_es$att.egt), c (csa_prov_spsp51_es$overall.se, csa_prov_spsp51_es$se.egt), csa_prov_spsp51_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp52_es$egt), c (csa_prov_spsp52_es$overall.att, csa_prov_spsp52_es$att.egt), c (csa_prov_spsp52_es$overall.se, csa_prov_spsp52_es$se.egt), csa_prov_spsp52_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp61_es$egt), c (csa_prov_spsp61_es$overall.att, csa_prov_spsp61_es$att.egt), c (csa_prov_spsp61_es$overall.se, csa_prov_spsp61_es$se.egt), csa_prov_spsp61_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp62_es$egt), c (csa_prov_spsp62_es$overall.att, csa_prov_spsp62_es$att.egt), c (csa_prov_spsp62_es$overall.se, csa_prov_spsp62_es$se.egt), csa_prov_spsp62_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp71_es$egt), c (csa_prov_spsp71_es$overall.att, csa_prov_spsp71_es$att.egt), c (csa_prov_spsp71_es$overall.se, csa_prov_spsp71_es$se.egt), csa_prov_spsp71_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp72_es$egt), c (csa_prov_spsp72_es$overall.att, csa_prov_spsp72_es$att.egt), c (csa_prov_spsp72_es$overall.se, csa_prov_spsp72_es$se.egt), csa_prov_spsp72_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp81_es$egt), c (csa_prov_spsp81_es$overall.att, csa_prov_spsp81_es$att.egt), c (csa_prov_spsp81_es$overall.se, csa_prov_spsp81_es$se.egt), csa_prov_spsp81_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp82_es$egt), c (csa_prov_spsp82_es$overall.att, csa_prov_spsp82_es$att.egt), c (csa_prov_spsp82_es$overall.se, csa_prov_spsp82_es$se.egt), csa_prov_spsp82_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp91_es$egt), c (csa_prov_spsp91_es$overall.att, csa_prov_spsp91_es$att.egt), c (csa_prov_spsp91_es$overall.se, csa_prov_spsp91_es$se.egt), csa_prov_spsp91_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spsp92_es$egt), c (csa_prov_spsp92_es$overall.att, csa_prov_spsp92_es$att.egt), c (csa_prov_spsp92_es$overall.se, csa_prov_spsp92_es$se.egt), csa_prov_spsp92_es$crit.val.egt)
    
    # same party, different person
    , bind_cols (c (NA, csa_prov_spdp31_es$egt), c (csa_prov_spdp31_es$overall.att, csa_prov_spdp31_es$att.egt), c (csa_prov_spdp31_es$overall.se, csa_prov_spdp31_es$se.egt), csa_prov_spdp31_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp32_es$egt), c (csa_prov_spdp32_es$overall.att, csa_prov_spdp32_es$att.egt), c (csa_prov_spdp32_es$overall.se, csa_prov_spdp32_es$se.egt), csa_prov_spdp32_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp41_es$egt), c (csa_prov_spdp41_es$overall.att, csa_prov_spdp41_es$att.egt), c (csa_prov_spdp41_es$overall.se, csa_prov_spdp41_es$se.egt), csa_prov_spdp41_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp42_es$egt), c (csa_prov_spdp42_es$overall.att, csa_prov_spdp42_es$att.egt), c (csa_prov_spdp42_es$overall.se, csa_prov_spdp42_es$se.egt), csa_prov_spdp42_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp51_es$egt), c (csa_prov_spdp51_es$overall.att, csa_prov_spdp51_es$att.egt), c (csa_prov_spdp51_es$overall.se, csa_prov_spdp51_es$se.egt), csa_prov_spdp51_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp52_es$egt), c (csa_prov_spdp52_es$overall.att, csa_prov_spdp52_es$att.egt), c (csa_prov_spdp52_es$overall.se, csa_prov_spdp52_es$se.egt), csa_prov_spdp52_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp61_es$egt), c (csa_prov_spdp61_es$overall.att, csa_prov_spdp61_es$att.egt), c (csa_prov_spdp61_es$overall.se, csa_prov_spdp61_es$se.egt), csa_prov_spdp61_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp62_es$egt), c (csa_prov_spdp62_es$overall.att, csa_prov_spdp62_es$att.egt), c (csa_prov_spdp62_es$overall.se, csa_prov_spdp62_es$se.egt), csa_prov_spdp62_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp71_es$egt), c (csa_prov_spdp71_es$overall.att, csa_prov_spdp71_es$att.egt), c (csa_prov_spdp71_es$overall.se, csa_prov_spdp71_es$se.egt), csa_prov_spdp71_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp72_es$egt), c (csa_prov_spdp72_es$overall.att, csa_prov_spdp72_es$att.egt), c (csa_prov_spdp72_es$overall.se, csa_prov_spdp72_es$se.egt), csa_prov_spdp72_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp81_es$egt), c (csa_prov_spdp81_es$overall.att, csa_prov_spdp81_es$att.egt), c (csa_prov_spdp81_es$overall.se, csa_prov_spdp81_es$se.egt), csa_prov_spdp81_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp82_es$egt), c (csa_prov_spdp82_es$overall.att, csa_prov_spdp82_es$att.egt), c (csa_prov_spdp82_es$overall.se, csa_prov_spdp82_es$se.egt), csa_prov_spdp82_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp91_es$egt), c (csa_prov_spdp91_es$overall.att, csa_prov_spdp91_es$att.egt), c (csa_prov_spdp91_es$overall.se, csa_prov_spdp91_es$se.egt), csa_prov_spdp91_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_spdp92_es$egt), c (csa_prov_spdp92_es$overall.att, csa_prov_spdp92_es$att.egt), c (csa_prov_spdp92_es$overall.se, csa_prov_spdp92_es$se.egt), csa_prov_spdp92_es$crit.val.egt)
    
    # different party, different person
    , bind_cols (c (NA, csa_prov_dpdp31_es$egt), c (csa_prov_dpdp31_es$overall.att, csa_prov_dpdp31_es$att.egt), c (csa_prov_dpdp31_es$overall.se, csa_prov_dpdp31_es$se.egt), csa_prov_dpdp31_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp32_es$egt), c (csa_prov_dpdp32_es$overall.att, csa_prov_dpdp32_es$att.egt), c (csa_prov_dpdp32_es$overall.se, csa_prov_dpdp32_es$se.egt), csa_prov_dpdp32_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp41_es$egt), c (csa_prov_dpdp41_es$overall.att, csa_prov_dpdp41_es$att.egt), c (csa_prov_dpdp41_es$overall.se, csa_prov_dpdp41_es$se.egt), csa_prov_dpdp41_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp42_es$egt), c (csa_prov_dpdp42_es$overall.att, csa_prov_dpdp42_es$att.egt), c (csa_prov_dpdp42_es$overall.se, csa_prov_dpdp42_es$se.egt), csa_prov_dpdp42_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp51_es$egt), c (csa_prov_dpdp51_es$overall.att, csa_prov_dpdp51_es$att.egt), c (csa_prov_dpdp51_es$overall.se, csa_prov_dpdp51_es$se.egt), csa_prov_dpdp51_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp52_es$egt), c (csa_prov_dpdp52_es$overall.att, csa_prov_dpdp52_es$att.egt), c (csa_prov_dpdp52_es$overall.se, csa_prov_dpdp52_es$se.egt), csa_prov_dpdp52_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp61_es$egt), c (csa_prov_dpdp61_es$overall.att, csa_prov_dpdp61_es$att.egt), c (csa_prov_dpdp61_es$overall.se, csa_prov_dpdp61_es$se.egt), csa_prov_dpdp61_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp62_es$egt), c (csa_prov_dpdp62_es$overall.att, csa_prov_dpdp62_es$att.egt), c (csa_prov_dpdp62_es$overall.se, csa_prov_dpdp62_es$se.egt), csa_prov_dpdp62_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp71_es$egt), c (csa_prov_dpdp71_es$overall.att, csa_prov_dpdp71_es$att.egt), c (csa_prov_dpdp71_es$overall.se, csa_prov_dpdp71_es$se.egt), csa_prov_dpdp71_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp72_es$egt), c (csa_prov_dpdp72_es$overall.att, csa_prov_dpdp72_es$att.egt), c (csa_prov_dpdp72_es$overall.se, csa_prov_dpdp72_es$se.egt), csa_prov_dpdp72_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp81_es$egt), c (csa_prov_dpdp81_es$overall.att, csa_prov_dpdp81_es$att.egt), c (csa_prov_dpdp81_es$overall.se, csa_prov_dpdp81_es$se.egt), csa_prov_dpdp81_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp82_es$egt), c (csa_prov_dpdp82_es$overall.att, csa_prov_dpdp82_es$att.egt), c (csa_prov_dpdp82_es$overall.se, csa_prov_dpdp82_es$se.egt), csa_prov_dpdp82_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp91_es$egt), c (csa_prov_dpdp91_es$overall.att, csa_prov_dpdp91_es$att.egt), c (csa_prov_dpdp91_es$overall.se, csa_prov_dpdp91_es$se.egt), csa_prov_dpdp91_es$crit.val.egt)
    , bind_cols (c (NA, csa_prov_dpdp92_es$egt), c (csa_prov_dpdp92_es$overall.att, csa_prov_dpdp92_es$att.egt), c (csa_prov_dpdp92_es$overall.se, csa_prov_dpdp92_es$se.egt), csa_prov_dpdp92_es$crit.val.egt)
    )) %>% setNames (., c ('level', 'treat', 'interaction', 'period', 'estim', 'se', 'crit_val')) %>% mutate (
      level = factor (level, levels=c ('alternation at the\nnational level', 'alternation at the\nprovincial level'))
  , treat = factor (treat, levels=c ('same party,\nsame person', 'same party,\ndifferent person', 'different party,\ndifferent person'))
  , interaction = factor (interaction, levels=c (
    'placed 1st in party list', 'placed 2nd or lower in party list'
    , 'not vulnerable', 'vulnerable'
    , 'previous executive\nor legislative experience', 'no previous experience'
    , 'committee chair', 'no committee chairmanship'
    , 'elected in midterm year', 'elected in concurrent year'
    , 'PJ', 'UCR'
    , 'man', 'woman'))
  , period = ifelse (interaction %in% c ('placed 1st in party list', 'not vulnerable', 'previous executive\nor legislative experience', 'committee chair', 'elected in midterm year', 'PJ', 'man'), period-0.125, period+0.125)
  , low95 = estim - se*crit_val
  , high95 = estim + se*crit_val)
summary (csa_estims_sub)



### (5.3.2.3) drawing the plots ####

## full sample
(pCSA_full <- ggplot (filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period))
                      , aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (color='gray35')
  
  # disaggregating the effect by type of posterior office
  + geom_point (data=filter (csa_estims_full, outcome!='resignation (0/100)' & !is.na (period))
                , aes (x=period, y=estim, color=outcome), size=0.95)
  + geom_errorbar (data=filter (csa_estims_full, outcome!='resignation (0/100)' & !is.na (period))
                   , aes (ymin=low95, ymax=high95, color=outcome), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-3, 0, 3, 6, 9), labels=c (-3, 0, 3, 6, 9))
  + coord_cartesian (ylim=c (-3, 9))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9))
 )


## first in party list
summary (filter (csa_estims_sub, crit_val < 10 & !is.na (period) & interaction %in% c ('placed 1st in party list', 'placed 2nd or lower in party list')))

(pCSA_list <- ggplot (filter (csa_estims_sub, !is.na (period) &
                                interaction %in% c ('placed 1st in party list', 'placed 2nd or lower in party list')), aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), color='gray35')
  
  # disaggregating the effect
  + geom_point (aes (color=interaction), size=0.95)
  + geom_errorbar (aes (ymin=low95, ymax=high95, color=interaction), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-3, 0, 3, 6, 9), labels=c (-3, 0, 3, 6, 9))
  + coord_cartesian (ylim=c (-4.5, 11))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## electoral vulnerability
summary (filter (csa_estims_sub, crit_val < 10 & !is.na (period) & interaction %in% c ('not vulnerable', 'vulnerable')))

(pCSA_vulnerable <- ggplot (filter (csa_estims_sub, !is.na (period) &
                                interaction %in% c ('not vulnerable', 'vulnerable')), aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), color='gray35')
  
  # disaggregating the effect
  + geom_point (aes (color=interaction), size=0.95)
  + geom_errorbar (aes (ymin=low95, ymax=high95, color=interaction), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-3, 0, 3, 6, 9), labels=c (-3, 0, 3, 6, 9))
  + coord_cartesian (ylim=c (-4.5, 11))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## previous experience
summary (filter (csa_estims_sub, crit_val < 10 & !is.na (period) & interaction %in% c ('previous executive\nor legislative experience', 'no previous experience')))

(pCSA_experience <- ggplot (filter (csa_estims_sub, !is.na (period) &
                                      interaction %in% c ('previous executive\nor legislative experience', 'no previous experience')), aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), color='gray35')
  
  # disaggregating the effect
  + geom_point (aes (color=interaction), size=0.95)
  + geom_errorbar (aes (ymin=low95, ymax=high95, color=interaction), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-3, 0, 3, 6, 9), labels=c (-3, 0, 3, 6, 9))
  + coord_cartesian (ylim=c (-4.5, 11))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## committee chair
summary (filter (csa_estims_sub, crit_val < 10 & !is.na (period) & interaction %in% c ('committee chair', 'no committee chairmanship')))

(pCSA_chair <- ggplot (filter (csa_estims_sub, !is.na (period) &
                                      interaction %in% c ('committee chair', 'no committee chairmanship')), aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), color='gray35')
  
  # disaggregating the effect
  + geom_point (aes (color=interaction), size=0.95)
  + geom_errorbar (aes (ymin=low95, ymax=high95, color=interaction), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-3, 0, 3, 6, 9), labels=c (-3, 0, 3, 6, 9))
  + coord_cartesian (ylim=c (-4.5, 11))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## elected in midterm
summary (filter (csa_estims_sub, crit_val < 10 & !is.na (period) & interaction %in% c ('elected in midterm year', 'elected in concurrent year')))

(pCSA_midterm <- ggplot (filter (csa_estims_sub, !is.na (period) &
                                      interaction %in% c ('elected in midterm year', 'elected in concurrent year')), aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), color='gray35')
  
  # disaggregating the effect
  + geom_point (aes (color=interaction), size=0.95)
  + geom_errorbar (aes (ymin=low95, ymax=high95, color=interaction), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-6, -3, 0, 3, 6, 9, 12), labels=c (-6, -3, 0, 3, 6, 9, 12))
  + coord_cartesian (ylim=c (-7.5, 13.5))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## party ID: PJ or UCR
summary (filter (csa_estims_sub, crit_val < 10 & !is.na (period) & interaction %in% c ('PJ', 'UCR')))

(pCSA_party <- ggplot (filter (csa_estims_sub, !is.na (period) &
                                      interaction %in% c ('PJ', 'UCR')), aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), color='gray35')
  
  # disaggregating the effect
  + geom_point (aes (color=interaction), size=0.95)
  + geom_errorbar (aes (ymin=low95, ymax=high95, color=interaction), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-3, 0, 3, 6, 9), labels=c (-3, 0, 3, 6, 9))
  + coord_cartesian (ylim=c (-4.5, 11))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## female
summary (filter (csa_estims_sub, crit_val < 10 & !is.na (period) & interaction %in% c ('man', 'woman')))

(pCSA_female <- ggplot (filter (csa_estims_sub, !is.na (period) &
                                      interaction %in% c ('man', 'woman')), aes (x=period, y=estim))
  + geom_hline (yintercept=0, linetype=3)
  + geom_vline (xintercept=0, linetype=3)
  
  # overall effect on resignation
  + geom_ribbon (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), aes (ymin=low95, ymax=high95), fill='gray85')
  + geom_line (data=filter (csa_estims_full, outcome=='resignation (0/100)' & !is.na (period)), color='gray35')
  
  # disaggregating the effect
  + geom_point (aes (color=interaction), size=0.95)
  + geom_errorbar (aes (ymin=low95, ymax=high95, color=interaction), width=0.1)
  
  # other graphical parameters
  + facet_grid (treat ~ level)
  + scale_y_continuous (breaks=c (-3, 0, 3, 6, 9), labels=c (-3, 0, 3, 6, 9))
  + coord_cartesian (ylim=c (-4.5, 11))
  + scale_x_continuous (breaks=c (-5:5), labels=c (-5:5))
  + scale_color_manual (values=movie)
  + ylab ('estimated effect')
  + xlab ('months relative to treatment\n(0 = month in which treatment began)')
  + theme_classic (base_size=cex_base)
  + theme (legend.position="bottom", legend.title=element_blank (), legend.text=element_text(size=cex_legend), legend.box.margin=margin(-9,-9,-9,-9)) )


## exporting
pwid <- 6
phei <- 5

ggsave ("figIKW_basic.png"
        , pIKW_basic, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figIKW_district.png"
        , pIKW_district, width=pwid, height=phei, units="in", dpi=600)

ggsave ("figCSA_full.png"
        , pCSA_full, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figCSA_list.png"
        , pCSA_list, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figCSA_vulnerable.png"
        , pCSA_vulnerable, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figCSA_experience.png"
        , pCSA_experience, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figCSA_chair.png"
        , pCSA_chair, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figCSA_midterm.png"
        , pCSA_midterm, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figCSA_party.png"
        , pCSA_party, width=pwid, height=phei, units="in", dpi=600)
ggsave ("figCSA_female.png"
        , pCSA_female, width=pwid, height=phei, units="in", dpi=600)




#### Saving --> if you want to access the results later, uncomment and run this code ####

# ## setting the working directory
# setwd (home)
# 
# system.time (save.image ("Results Resignations Argentina (monthly).RData"))
# system.time (load ("Results Resignations Argentina (monthly).RData"))

sink (NULL)
